Tôi đang sử dụng Python 3.5.1. Tôi đã đọc tài liệu và phần gói ở đây: https://docs.python.org/3/tutorial/modules.html#packages
Bây giờ, tôi có cấu trúc sau:
/home/wujek/Playground/a/b/module.py
module.py:
class Foo:
def __init__(self):
print('initializing Foo')
Bây giờ, trong khi ở /home/wujek/Playground:
~/Playground $ python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x100a8f0b8>
Tương tự, bây giờ ở nhà, siêu mạnh của Playground:
~ $ PYTHONPATH=Playground python3
>>> import a.b.module
>>> a.b.module.Foo()
initializing Foo
<a.b.module.Foo object at 0x10a5fee10>
Trên thực tế, tôi có thể làm tất cả các loại công cụ:
~ $ PYTHONPATH=Playground python3
>>> import a
>>> import a.b
>>> import Playground.a.b
Tại sao điều này làm việc? Tôi dù có cần thiết để được __init__.pyfile (những sản phẩm nào có thể làm việc) trong cả hai avà bcho module.pylà có thể nhập cảng khi đường dẫn trỏ Python cho Playgroundthư mục?
Điều này dường như đã thay đổi từ 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
Với __init__.pycả hai ~/Playground/avà ~/Playground/a/bnó hoạt động tốt.