I've been working on a python3 application and I ran into a strange problem that picked my curiosity after annoying me greatly.
My file structure is something like this:
root/
| __init__.py
| main.py
| fuzzy/
| __init__.py
| foo.py
| dreamy/
| __init__.py
| bar.py
| meh.py
So I need to use a method of the foo
module in both bar
and meh
.
In bar
I wrote:
from fuzzy.foo import foo_function
And that worked perfectly fine.
Now in meh
I used the exact same syntax but ended up with an import error:
ModuleNotFoundError: No module named 'fuzzy'
I managed to solve the problem using this method, but I would like to understand why this happens.
According to the Python3 documentation my import syntax is correct:
An alternative way of importing the submodule is:
from sound.effects import echo
This seems to also be the syntax provided by answered question on this forum
Moreover, both bar
and meh
are in the same directory and their code starts in exactly the same way.
Does anyone know why this error occurs in one file but not in the other ?
Is there something I did wrong ?
If I did do something wrong, what would be the correct way (or good practices) to import local packages / methods ?
Thanks for your insight.
---EDIT---
I did not fiddle with my PYTHONPATH
in any way, and I am running Python3 from a (vanilla) Conda venv. For good measures, I also created a new file structure and copied the code in new, blank files. This was to make sure that nothing "funny" would have happened to my directory.