[英]Problems importing python modules from other directories?
My project has been getting bigger and the correct modules are failing to import properly.我的项目越来越大,正确的模块无法正确导入。 Result is the program stops running at the first line.
结果是程序在第一行停止运行。 Here is what my directory map currently looks like:
这是我的目录映射目前的样子:
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
The problem that I am getting repeatedly, in many different forms is this:我以许多不同的形式反复遇到的问题是:
streamer.py流光.py
from editor import A_class
main1.py main1.py
import Sound_editor.streamer
When I run main1.py, it first imports streamer file.当我运行 main1.py 时,它首先导入流媒体文件。 Then the streamer file attempts and fails to import the editor file.
然后流媒体文件尝试导入编辑器文件失败。 error
错误
ModuleNotFoundError: No module named 'editor'
I don't know what else to do.我不知道还能做什么。 I've tried:
我试过了:
import PROTOTYPE.Sound_editor.editor
import PROTOTYPE.Sound_editor.editor
from Sound_editor import editor
as well as from Sound_editor.editor import A_class
from Sound_editor import editor
以及from Sound_editor.editor import A_class
What else should I try.我还应该尝试什么。 Do you experts see any obvious errors?
您的专家是否看到任何明显的错误?
Update 1更新 1
chepner recommends a relative import. chepner 建议使用相对导入。
from .editor import A_class
. from .editor import A_class
。 This was successful but caused another problem that requires expounding.这是成功的,但引起了另一个需要阐述的问题。
streamer.py also has the following import: from .openant.ant.easy.node import Node
but also node has imports too: node.py from ant.base.ant import Ant
error ModuleNotFoundError: No module named 'ant.base'
On first glance, it seems like the library I cloned from Github has some naming troubles. streamer.py 也有以下导入:
from .openant.ant.easy.node import Node
但节点也有导入: node.py from ant.base.ant import Ant
error ModuleNotFoundError: No module named 'ant.base'
On乍一看,我从 Github 克隆的库似乎有一些命名问题。 Folders and files with same names just sounds like a disaster.具有相同名称的文件夹和文件听起来像是一场灾难。 When I try using a dot here: ```from .ant.base.ant import Ant`` error
当我在这里尝试使用点时:```from .ant.base.ant import Ant``错误
ModuleNotFoundError: No module named 'Sound_editor.openant.ant.easy.ant'
Either:任何一个:
from .ant...
is not going up enough directories or from .ant...
没有进入足够的目录或from editor import A_class
is an absolute import. from editor import A_class
是绝对导入。 Python will only look in directories that appear in sys.path
for a module named editor
. Python只会在
sys.path
中出现的目录中sys.path
名为editor
的模块。 When you run main1.py
, Sound_editor
is found because it's in the same directory as main1.py
;当您运行
main1.py
, Sound_editor
被发现,因为它是在同一目录main1.py
; editor
is not. editor
不是。
What you want is a relative import, so that editor
is found in whatever package streamer
itself is found in:您想要的是相对导入,因此可以在以下任何包
streamer
找到该editor
:
from .editor import A_class
you can add to the Python path at runtime:您可以在运行时添加到 Python 路径:
import sys导入系统
sys.path.insert(1, '/path/to/application/app/folder') sys.path.insert(1, '/path/to/application/app/folder')
import file导入文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.