我使用的是Python 3.5.1.我在这里阅读了文档和包裹部分:https://docs.python.org/3/tutorial/modules.html#packages
现在,我有以下 struct :
/home/wujek/Playground/a/b/module.py
module.py
:
class Foo:
def __init__(self):
print('initializing Foo')
现在,在/home/wujek/Playground
岁的时候:
~/Playground $ python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x100a8f0b8>
同样,现在在家里,Playground
岁的超级富豪:
~ $ PYTHONPATH=Playground python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x10a5fee10>
实际上,我可以做各种各样的事情:
~ $ PYTHONPATH=Playground python3
>>> import a
>>> import a.b
>>> import Playground.a.b
为什么会这样?我认为a
和b
中都需要__init__.py
个文件(空的就可以了),当Python路径指向Playground
文件夹时,module.py
才能被导入?
这似乎与Python 2.7有所不同:
~ $ PYTHONPATH=Playground python
>>> import a
ImportError: No module named a
>>> import a.b
ImportError: No module named a.b
>>> import a.b.module
ImportError: No module named a.b.module
~/Playground/a
和~/Playground/a/b
中都有__init__.py
,效果很好.