gpt4 book ai didi

python - 没有名为 "file1.py"的模块; test1 不是一个包

转载 作者:行者123 更新时间:2023-11-28 21:36:17 30 4
gpt4 key购买 nike

我正在尝试模块化我的代码,但我遇到了问题。

我的文件夹是这样构造的:

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com