【发布时间】:2021-09-27 07:04:28
【问题描述】:
我的项目越来越大,正确的模块无法正确导入。结果是程序在第一行停止运行。这是我的目录映射当前的样子:
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
我以多种不同形式反复遇到的问题是:
streamer.py
from editor import A_class
main1.py
import Sound_editor.streamer
当我运行 main1.py 时,它首先导入流光文件。然后流媒体文件尝试导入编辑器文件但失败。 错误
ModuleNotFoundError: No module named 'editor'
我不知道还能做什么。我试过了:
- 本指南中有很多内容:https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html
- 点我通往正确道路的变体:
import PROTOTYPE.Sound_editor.editor - 使用来自:
from Sound_editor import editor以及from Sound_editor.editor import A_class - 我研究过这个答案:Importing files from different folder。我不确定他将目录构建为一个包是什么意思。我已经添加了 init.py 文件。 (它们是空的..)
我还应该尝试什么。专家们有没有发现任何明显的错误?
更新 1
chepner 建议相对导入。 from .editor import A_class。这是成功的,但导致了另一个需要阐述的问题。
streamer.py 也有以下导入:from .openant.ant.easy.node import Node 但节点也有导入:
node.py
from ant.base.ant import Ant
错误
ModuleNotFoundError: No module named 'ant.base'
乍一看,我从 Github 克隆的库似乎有一些命名问题。同名的文件夹和文件听起来就像一场灾难。当我在这里尝试使用点时:
```从.ant.base.ant导入蚂蚁`
错误
ModuleNotFoundError: No module named 'Sound_editor.openant.ant.easy.ant'
要么:
-
from .ant...没有足够的目录或 - 名为 ant 的文件/文件夹混淆了命令...??
【问题讨论】: