我试图了解python模块系统,并且遇到了我完全不了解的行为。我有这个文件夹结构:
.
├── foo
│ ├── __init__.py
│ └── bar.py
├── playground
│ ├── __init__.py
│ ├── __main__.py
│ ├── playground.py
│ └── toolkit
│ ├── __init__.py
│ ├── __main__.py
│ └── toolkit.py
└── setup.py
3 directories, 9 files
在foo / bar.py内部是这样的:
import sys
def print_sys():
print(sys.path)
playground/playground.py
和playground/__main__.py
包含相同的代码:
from foo import bar
bar.print_sys()
from playground.toolkit.toolkit import say_something
say_something()
playground/toolkit/toolkit.py
和playground/toolkit/__main__.py
也包含相同的代码:
from foo import bar
bar.print_sys()
def say_something():
print("something")
if __name__ == "__main__":
say_something()
执行python -B playground/playground.py
后,我会得到一个ModuleNotFoundError: No module named 'playground.toolkit'; 'playground' is not a package
。这些是我的sys.path变量(用$ SRC替换了源文件夹):
['$SRC/Playground/playground', ..., '$SRC']
当我执行python -B -m playground
时,它也有效,python -B playground/toolkit/toolkit.py
和python -B -m playground.toolkit
有人可以在这里指出我的错误吗?