0

I have this problem with python, and I'm absolutely lost, I hope that some of you know a solution. Considering the project structure

main_folder
main.py
----second_folder
----a.py
----b.py

in main.py

import second_folder.a as bla

in a.py

import b as blub

When I run a.py everything works fine, but when I run main.py I get the error No module named 'b'. I tried literally everything, but I don't see a "clean" solution for this problem.

I know, this might be a duplicate, but I'm searching for weeks, and I'm really frustrated at this point :( I think python is a really cool language, but this problem makes it nearly unusable for large projects.

Bomel
  • 97
  • 1
  • 6
  • In `main.py` your import statement is fine. But in `a.py` it needs to be namespaced: `import second_folder.b as blub`. – wim Aug 06 '21 at 20:01
  • Maybe try adding an empty `__init__.py` file to second_folder? – purple Aug 06 '21 at 20:06
  • @wim I also tried that but then, I cannot run `a.py` gives me an import error, and I need to run `a.py` on it's own. – Bomel Aug 06 '21 at 20:14
  • @purple also tried that, without any success :( – Bomel Aug 06 '21 at 20:15
  • What about `from second_folder import a as bla`? – purple Aug 06 '21 at 20:19
  • @purple same :( `No module named 'second_folder'` – Bomel Aug 06 '21 at 20:27
  • You still have the empty `__init__` in second_folder correct? – purple Aug 06 '21 at 20:30
  • @purple yes I have – Bomel Aug 06 '21 at 20:34
  • From within the "main_folder", you could run `a.py` with the following command `python -m second_folder.a`. You can not run `a.py` directly as a script, since it needs to import from a "sibling" module and that is only supported from within a package, it's not supported for standalone scripts. – wim Aug 06 '21 at 20:48
  • @wim but this problem keeps propagating, even if I add my upper scenario as a subrepo and import main.py as a module. – Bomel Aug 06 '21 at 21:47

0 Answers0