1

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

from editor import A_class

main1.py

import Sound_editor.streamer

When I run main1.py, it first imports streamer file. 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
  2. variations of dotting my way to the right path: import PROTOTYPE.Sound_editor.editor
  3. using from: from Sound_editor import editor as well as from Sound_editor.editor import A_class
  4. I've studied this answer: 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. (They are empty..)

What else should I try. Do you experts see any obvious errors?

Update 1
chepner recommends a relative import. 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. 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

ModuleNotFoundError: No module named 'Sound_editor.openant.ant.easy.ant'

Either:

  1. from .ant... is not going up enough directories or
  2. The file/folder called ant is confusing the command...??

2 Answers2

3

from editor import A_class is an absolute import. Python will only look in directories that appear in sys.path for a module named editor. When you run main1.py, Sound_editor is found because it's in the same directory as main1.py; editor is not.

What you want is a relative import, so that editor is found in whatever package streamer itself is found in:

from .editor import A_class
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Another related issue emerged down the chain of imports. It's too much info to add here in the comments, so check update1. I also updated the directory I outlined. You got me past the first link in the chain. – Michael Korn Jul 19 '21 at 16:26
  • Are there `__init__.py` files missing from the packages, or did you omit them from the question for brevity? For example, without `easy/__init__.py`, `easy` is not a module. – chepner Jul 19 '21 at 16:30
  • Very briefly: a package is a module that can (or does; I'm not sure if there is a distinction) contain other modules. A module can be created from a file or a directory containing a `__init__.py` file. – chepner Jul 19 '21 at 16:33
  • I omitted /__init__.py for brevity. Just added them to the main post – Michael Korn Jul 19 '21 at 16:34
  • And `ant/`? It's not just the "terminal" directory that needs `__init__.py`; *all* directories along the way require them. – chepner Jul 19 '21 at 16:37
  • But `from ant.base.ant import Ant` is an absolute import as well, so you are back to the original problem again. – chepner Jul 19 '21 at 16:38
  • Correct, __init__.py is included all the way down, but excluded from the top level: PROTOTYPE. (next reply) from .ant.base.ant import Ant ---> ModuleNotFoundError: No module named 'Sound_editor.openant.ant.easy.ant' Which is interesting because we are importing .ant.base.and and it's looking in ant.easy.ant... – Michael Korn Jul 19 '21 at 16:41
  • Does `SoundEditor/__init__.py` exist? Seems like it should for `import Sound_Editor.streamer` to work, but it's quite possible I'm misexplaining or misunderstanding something as well. – chepner Jul 19 '21 at 16:45
  • `node.py` seems to assume that `openant` itself is on `sys.path`. It should be something like `from ..ant.base.ant import Ant`. – chepner Jul 19 '21 at 16:49
  • yes soundeditor has __init__.py. So I just added 3 dots. `from ...ant.base.ant import Ant` – Michael Korn Jul 19 '21 at 16:55
  • I think you've helped come down to the fundamental problem here: all of the directories in this library are screwed. I was hoping that there would be a more elegant solution than going into each module to update imports. Also, I expected that a library like this wouldn't have issues like this. – Michael Korn Jul 19 '21 at 16:57
  • The solution might be to put `PROTOTYPE/openant` directly into `sys.path`. The library appears to assume it will be installed somewhere already on the path, not embedded in another project. – chepner Jul 19 '21 at 17:03
  • So I just updated all the imports. No errors. But I think the sys.path solution might be more sustainable. Should I just follow the other answerer for that? – Michael Korn Jul 19 '21 at 17:07
  • I would install the library properly (in a virtual environment), rather than trying to import from an embedded directory. – chepner Jul 19 '21 at 17:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235074/discussion-between-michael-korn-and-chepner). – Michael Korn Jul 19 '21 at 19:52
0

you can add to the Python path at runtime:

some_file.py

import sys

insert at 1, 0 is the script path (or '' in REPL)

sys.path.insert(1, '/path/to/application/app/folder')

import file

Sul
  • 1