0

My Python project is currently suffering from issues where my dependencies have conflicts. I wanted to solve that later, after I finished writing tests for a specific function in my code (function_a.py in file_a.py).

When I run pytest explicitly on the test file in this directory (pytest test_a.py) everything is fine and my tests pass. But if I add an __init__.py file to this directory my code errors due to the conflict between the dependencies (which are imported in the __init__.py file of the parent directory).

Works:

- parent_directory
 - other_files.py
 - __init__.py
 - child_directory
  - file_a.py
  - test_a.py
  

Does not work:

- parent_directory
 - other_files.py
 - __init__.py
 - child_directory
  - file_a.py
  - test_a.py
  - __init__.py

Why does the __init__.py in the child_directory causes my other __init__.py in the parent_directory to be executed?

C Murphy
  • 313
  • 2
  • 11
  • `__init__.py` files (I'll update the post) – C Murphy Sep 27 '21 at 14:07
  • Does this answer your question? [Pytest: how to work around missing ``__init__.py`` in the tests folder?](https://stackoverflow.com/questions/50796370/pytest-how-to-work-around-missing-init-py-in-the-tests-folder) – MisterMiyagi Sep 27 '21 at 14:12
  • Not really. My main question is less to do with pytest, and more to do with why is having a `__init__.py` in my child directory causing the` __init__.py` in my parent directory to be called? Is it because the child_dir is a submodule to the parent_dir when `__init__.py` is present? And when a submodule is loaded, the module (parent) gets loaded too? – C Murphy Sep 27 '21 at 14:43
  • 1
    Yes, this is exactly what *pytest* is doing as part of its automatic test execution. – MisterMiyagi Sep 27 '21 at 14:45
  • Ah okay, starting to understand this now! So if i was just running `test_a.py` as a python file (`python test_a.py`) I would not be experiencing this issue? – C Murphy Sep 27 '21 at 14:59

0 Answers0