Python在“import”上找不到模块(最小项目,没有外部导入)

2024-04-29 04:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我这里有个奇怪的错误。Python无法导入模块,尽管我清楚地看到它在那里。这也不是一个复杂的设置与许多路径,但只是一个小项目。你知道吗

我运行test.py。它导入模块stencils。在加载期间,调用stencils/__init.py__。此文件找不到模块Stencil,它是stencils/Stencil.py

此外:

  • 直接运行stencils/__init__.py:工作。你知道吗
  • 运行stencils/Abc.py:工作。你知道吗
  • 运行test.py:给出错误:
Traceback (most recent call last):   File "/Users/mi/stencilcodegen/test.py", line 1, in <module>
import stencils   File "/Users/mi/stencilcodegen/stencils/__init__.py", line 1, in <module>
import Stencil ImportError: No module named 'Stencil'

\uu初始化\uuuuuuuuuuuuuuuuuuuuuuy:

import Stencil
import Abc

Abc.py公司:

import Stencil
stencil = Stencil.Stencil

模具.py:(可能不相关,因为它根本找不到模块。)

class Stencil:
    def __init__(self):
        a = 1;

测试.py:

import stencils
a = stencil.Stencil

目录截图(PyCharm)

enter image description here


Tags: 模块pytestimportinit错误lineusers
1条回答
网友
1楼 · 发布于 2024-04-29 04:28:18

我也遇到过类似的问题,我只是碰巧找到了解决办法。你知道吗

你的test.py文件是'根',所以当你在那里使用任何代码时,你必须从这个角度看层次结构。必须在子文件夹中导入以下文件:

在uuu init_uuy.py中,例如:

import stencils.Stencil as Stencil
import stencils.Abc as Abc

由于您要导入模块和函数以及文件夹stencils中的任何内容,因此仍然必须为存储函数或任何内容的位置提供清晰的路径。对不起,我的表达不好,我是德国人。你知道吗

相关问题 更多 >