Reputation: 321
I am running into an error that I am struggling to debug and am very confused.
I have a folder structure like this:
|_all_tests.py
|_main.py
|_pitches
|__zones.py
|__pitches.py
|__errors.py
In zones I import errors, and when I run the zones file there are no errors. However, I import zones from main, and when I import zones from main I get the ModuleNotFoundError: No module named 'errors'.
I am confused why this is the case. When I run zones I get no errors, but when I import zones (and hence run the code) I get this ModuleNotFoundError... can someone help explain what is going on?
Upvotes: 0
Views: 9899
Reputation: 91
When you run zones.py
directly, python searches for modules relative to the address zones.py
is in, therefore, it can find errors
as it is in the same directory.
However, when you run main.py
, python starts looking for the modules you import inside the folder main.py
is in, even if your import line is inside another file in another directory. If you plan to run main.py
, then you should change your import lines to be relative to your project root address. For example, change import errors
in zones.py
to import pitches.errors
.
You should also add a __init__.py
file to your pitches folder in this case. It can be empty.
Upvotes: 1
Reputation: 78
Add to /pitches folder the file __ init __.py to indicate python that the folder is a package.
Upvotes: 1
Reputation: 51
since the way you import them is important, look at Importing Python modules from different levels of hierarchy . it might help
Upvotes: 2