我的程序不能识别beetwen模块&核心我在python2上

2024-10-03 19:28:30 发布

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

我有一个python程序,其中出现以下错误:

ImportError: No module named core

导致错误的导入是:

from core import wcolors

文件wcolors.py位于名为core的目录中,还有另一个名为modules的目录,因此当我运行程序时,它会给出以下错误输出:

Traceback (most recent call last):
File "anubis.py", line 7, in <module>
from core import wcolors
ImportError: No module named core

目录结构

dir结构如下

anubis
--anubis.py    (the script that i run)
--core
  --wcolors.py (the file i import from core)

-- modules
   [the modules i suposed to load during the execution.]

作为另一个细节,core中的所有文件都是用.pyc扩展名编译的。你知道吗


Tags: thenofrompycoreimport程序目录
2条回答

你可以试试这个:

from anubis.core import wcolors

或者您可以更改“core”的名称,它可以是django和python的关键字。你知道吗

您只需要在anubisanubis/core目录中添加一个空的__init__.py,这样应该可以。如果没有__init__.py文件,python不会认为目录是一个模块。你知道吗

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file

Python docs

相关问题 更多 >