我正在尝试模块化我的代码,但我遇到了问题。
我的文件夹是这样构造的:
code
|_main.py
|_test1
|_calcA.py (which contains a method)
|_test2
|_calcB.py (which contains another method)
|_test3
|_calcC.py (which contains another method)
现在我的 main.py 包含这些行:
import sys; import pprint
pprint.pprint(sys.path)
from test1.calccircle.py import calcA
from test2.calctriangle.py import calcB
from test3.calccarre.py import calcB
出现以下错误:
ImportError: No module named 'test1.calcA.py'; 'test1.calcA' is not a package
最佳答案
您无需指定 .py
在导入模块时。 Python 知道您的模块只是 Python 代码。所以删除 .py
在 Python 中导入模块时。
关于python - 没有名为 "file1.py"的模块; test1 不是一个包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244200/