最佳答案
                                        
                                                                        
                                我使用的是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中:
~/Playground $ python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x100a8f0b8>
类似地,现在在home, 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
为什么会这样?我虽然需要__init__.py文件(空的会工作)在a和b中,当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可以正常工作。
 
                                
                             
                                
                             
                                
                             
                                
                            