我在python教程中看到了这个:
需要__init__.py文件才能使Python将目录视为包含包;
我在pycharm中创建了这样的目录层次结构,其中subdir1不包含__init__.py,而subdir2包含__init__.py文件.

首先,我将Directory添加到system.pyth中.
我分别在hello1.py和hello2.py中编写了一个hello函数.
然后我在测试文件中调用hello func,如下所示:
# test1.py
from subdir1 import hello1
hello1.hello()
# test2.py
from subdir2 import hello2
hello2.hello()
Run Code Online (Sandbox Code Playgroud)
他们都成功了.对于来自不同目录的导入模块,似乎不需要__init__.py,对吧?
所以,我想知道在什么情况下需要__init__.py.谢谢你的回答!
| 归档时间: |
|
| 查看次数: |
968 次 |
| 最近记录: |