everwitt7
everwitt7

Reputation: 321

Python ModuleNotFoundError No module named 'custom'

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

Answers (3)

Arash Moayyedi
Arash Moayyedi

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

Jesus Sono
Jesus Sono

Reputation: 78

Add to /pitches folder the file __ init __.py to indicate python that the folder is a package.

Upvotes: 1

Alireza13613
Alireza13613

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

Related Questions