我使用的是Python 3.5.1。我在这里阅读了文件和包装部分:
https://docs.python.org/3/tutorial/modules.html#packages
现在我有以下结构:
/home/wujek/Playground/a/b/module.py
module.py:
class Foo:
def __init__(self):
print('initializing Foo')
现在,在/ home / wujek /游乐场:
~/Playground $ python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
同样的,现在在家里,操场的超级文件夹:
~ $ PYTHONPATH=Playground python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
其实我可以做各种各样的事情:
~ $ PYTHONPATH=Playground python3
>>> import a
>>> import a.b
>>> import Playground.a.b
为什么这个工作?当Python路径指向Playground文件夹时,尽管在module.py中的a和b都需要__init__.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
__init__.py在〜/ Playground / a和〜/ Playground / a / b中都可以正常工作。