我的项目越来越大,正确的模块无法正确导入。结果是程序在第一行停止运行。以下是我的目录映射当前的外观:
PROTOTYPE
- Sound_editor (folder)
- - openant (cloned library from Github)
- - - __init__.py
- - - (a bunch of files and folders from the library)
**
- - - ant
- - - - base
- - - - - ant.py
- - - - - __init__.py
- - - - easy
- - - - - __init__.py
- - - - - node.py
**
- - - demo.py
- - __init__.py
- - editor.py
- - reader.py
- - streamer.py
- - main2.py
- main1.py
在许多不同的形式中,我反复遇到的问题是:
拖缆.py
from editor import A_class
main1.py
import Sound_editor.streamer
当我运行main1.py时,它首先导入拖缆文件。然后拖缆文件尝试导入编辑器文件,但失败。 错误
ModuleNotFoundError: No module named 'editor'
我不知道还能做什么。我试过:
import PROTOTYPE.Sound_editor.editor
from Sound_editor import editor
以及from Sound_editor.editor import A_class
我还应该尝试什么。您的专家是否看到任何明显的错误
更新1
切普纳建议使用相对导入from .editor import A_class
。这是成功的,但引起了另一个需要阐述的问题
streamer.py还具有以下导入:from .openant.ant.easy.node import Node
但节点也具有导入:
node.pyfrom ant.base.ant import Ant
错误ModuleNotFoundError: No module named 'ant.base'
乍一看,我从Github克隆的库似乎有一些命名问题。同名文件夹和文件听起来像是一场灾难。当我尝试在此处使用点时:
```from.ant.base.ant导入ant``
错误
ModuleNotFoundError: No module named 'Sound_editor.openant.ant.easy.ant'
要么:
from .ant...
上的目录不够,或者
您可以在运行时将以下内容添加到Python路径:
some_file.py
导入系统
insert位于1,0是脚本路径(或REPL中的“”)
sys.path.insert(1,“/path/to/application/app/folder”)
导入文件
from editor import A_class
是绝对重要的。Python将仅在sys.path
中出现的目录中查找名为editor
的模块。运行main1.py
时,会找到Sound_editor
,因为它与main1.py
位于同一目录中editor
不是您需要的是一个相对导入,这样
editor
就可以在任何包streamer
中找到:相关问题 更多 >
编程相关推荐