简体   繁体   English

从其他目录导入 python 模块时出现问题?

[英]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:我试过了:

  1. So many things from this guide: https://chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html本指南中有很多内容: https : //chrisyeh96.github.io/2017/08/08/definitive-guide-python-imports.html
  2. variations of dotting my way to the right path: import PROTOTYPE.Sound_editor.editor将我的方式加点到正确路径的变体: import PROTOTYPE.Sound_editor.editor
  3. using from: from Sound_editor import editor as well as from Sound_editor.editor import A_class使用 from: from Sound_editor import editor以及from Sound_editor.editor import A_class
  4. I've studied this answer: Importing files from different folder .我研究过这个答案: Importing files from different folder I'm not sure what he means by structuring directories as a package.我不确定他将目录构建为包是什么意思。 I have already added init .py files.我已经添加了init .py 文件。 (They are empty..) (他们是空的..)

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:任何一个:

  1. from .ant... is not going up enough directories or from .ant...没有进入足够的目录或
  2. The file/folder called ant is confusing the command...??名为 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.pySound_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 路径:

some_file.py some_file.py

import sys导入系统

insert at 1, 0 is the script path (or '' in REPL)在 1 处插入,0 是脚本路径(或 REPL 中的 '')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM