Maciej Dałek
Maciej Dałek

Reputation: 69

Problem when importing module nested in the same root folder

I have my_super_root folder with such tree:

my_super_root:
   training_folder:
          my_subfolder:
              conftest.py
              submodule.py

and the code in conftest.py is just:

from training_folder.my_subfolder import submodule

when I run pytest from my_super_root level (it is set as a current working directory) I receive an error:

from training_folder.my_subfolder import my_submodule
E   ModuleNotFoundError: No module named 'training_folder'

However when I move conftest to the my_super_root and run pytest once again it works well. Why does it happen if folder in which you call pytest is added to sys.path?

Upvotes: 0

Views: 41

Answers (1)

mino.g
mino.g

Reputation: 25

From the Python 3 Modules documentation:

"The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string, unintentionally hiding valid modules that occur later on the module search path."

Upvotes: 1

Related Questions