【发布时间】:2021-05-17 06:37:11
【问题描述】:
我正在编辑以最小化为可重现的示例:
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和M1是同级的吗?
-
你是如何运行
cal.py的?从命令行?从哪个目录? -
我愿意
export PYTHONPATH=/PATH/TO/M1;我使用python cal.py运行 -
你的目录结构中的 cal.py 在哪里?
-
我相信stackoverflow.com/a/8899345 和section 6.4 from the Python docs 一起回答了您的问题。它还有助于检查
sys.modules以找出已加载的模块,并使用dir()查看它们定义的名称。
标签: python import importerror nameerror