【发布时间】:2020-10-03 10:12:57
【问题描述】:
我在导入模块时遇到问题,我使用 Spyder 3.7 作为编辑器,看起来好像没有导入: 第一个模块 test.py :
def func():
print('func() is tes.py')
print("top level in test.py")
if __name__=='__main__':
print('test.py is being run directly')
else:
print('test.py is being imported into another module')
其次是test2.py
import test
print ('top level in test2.py')
test.func()
if __name__=='__main__':
print('test.py is being run directly')
else:
print('test.py is being imported into another module')
两个文件都注册在同一个文件夹中 当我执行 test2.py 时出现此错误
模块'test'没有属性'func'
【问题讨论】:
-
你可能想给你的模块命名,因为
test是一个标准的Python模块。可能是您正在导入该文件,而不是您自己的test.py文件。 -
是的@khelwood,这就是原因。我将两个文件的名称都更改为一个和两个它可以正常执行。谢谢
-
太棒了。我会发布它作为答案。
-
我运行了你的代码 (
python test2.py) 并且它有效。没有错误。无法重现。