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>
마찬가지로, 이제 집에서 다음과 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대한 module.py임포트 될 때까지 파이썬 경로 지점 Playground폴더?
이것은 파이썬 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잘 작동합니다.