从文件夹导入文件.py

2024-05-13 01:35:23 发布

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

我有个小问题。你知道吗

这是我的结构:

项目经理:

ode :

    ODESolvers.py

    util.py

    __init__.py

draw :

    DrawTools.py

    Shapes.py

    __init__.py

demo_project.py

demo_project.py中,我想导入utilDrawTools

所以我在demo_project.pyfrom draw import DrawTools中写了,但是我有一个错误:“ImportError:没有名为'DrawTools'的模块”。你知道吗

我也试过:

import sys

sys.path.append("C:\\Users\\Ayyoub\\Desktop\\Développement logiciel\\Projet_a_remplir\\draw")

from DrawTools import *

还是同样的错误。。你知道吗

你能帮我做这个吗?你知道吗


Tags: frompyimportprojectinitdemo错误util
2条回答

您需要在每个子文件夹odedraw中添加一个名为__init__.py的(空)文件。你知道吗

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 (deeper) on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

另请参阅packages上的文档。你知道吗

导入区分大小写。将from draw import Drawtools更改为from draw import DrawTools。你知道吗

相关问题 更多 >