我正在编辑以最小化为可重现的示例:
cal.py 的内容
import M1
M1.SM1.nice_hello.hello()
目录结构:
M1/
├── __init__.py
└── SM1
├── __init__.py
└── nice_hello.py
SM1/nice_hello.py 的内容:
def hello():
print(f'Hello my friend!!!')
所有其他文件(init.py 文件)都是空的。
运行 cal.py:
export PYTHONPATH=/PATH/TO/M1 ; python cal.py
但这给了我以下错误:
Traceback (most recent call last):
File "cal.py", line 3, in <module>
M1.SM1.nice_hello.hello()
AttributeError: module 'M1' has no attribute 'SM1'
最佳答案
如果您导入整个模块名称,即在文件cal.py
中,它应该可以工作
import M1.SM1.nice_hello
M1.SM1.nice_hello.hello()
关于python - 为什么主模块看不到里面的子模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67565002/