没有名为“ file1.py”的模块;test1不是软件包

Liq*_*ake 2 python python-3.x

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

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

code
   |_main.py
   |_test1
           |_calcA.py (which contains a method)
   |_test2
           |_calcB.py (which contains another method)
   |_test3
           |_calcC.py (which contains another method)
Run Code Online (Sandbox Code Playgroud)

现在我的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
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

ImportError:没有名为“ test1.calcA.py”的模块;'test1.calcA'不是一个软件包

Muf*_*eed 6

.py导入模块时无需指定。Python知道您的模块仅是Python代码。因此.py,在使用Python导入模块时删除。