0

Here is the structure:

-app-l1
  -src
    -__init__.py
    -my_module.py
  -tests
    -__init__.py
    -test_my_module.py

I'm trying to import all functions from my_module to test_my_module.py for unittesting.

I've tried: from ..src.my_module import my_module which returns ImportError: attempted relative import with no known parent package

Also tried: from src.my_module import my_module which returns ModuleNotFoundError: No module named 'src.my_module'

Printing out sys.pathshows that it's only looking inside tests directory. How do I correctly specify where to look for the module?

Any help would be really nice! Thanks

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
  • 1
    Does this answer your question? [Importing modules from parent folder](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) – Gugu72 Oct 21 '20 at 15:24
  • @Gugu72 I'm trying to find a way that wouldn't include sys.path modifications – KKarpac Oct 21 '20 at 15:28
  • If you read the answers, there is one: https://stackoverflow.com/a/50194143/12134355 – Gugu72 Oct 21 '20 at 15:31
  • But that involves a virtual environment and install my_module with pip. Isn't there a simpler way? – KKarpac Oct 21 '20 at 15:36
  • Well, you should already have a venv for each projects. And no, if there's an easier way we would have told you instead of giving hard ways, isn't it? – Gugu72 Oct 21 '20 at 15:40
  • Add an empty `__init__.py` under `app-l1`. Then going up one level with `..` will work. – progmatico Oct 21 '20 at 17:45

1 Answers1

0

You could set the PYTHONPATH. Python will look for modules in all the paths you added to your PYTHONPATH variable. You can save it in your bashrc if you don't want to set it each time again. If you like conda env you can also add variables to your env like this:

conda env config vars set PYTHONPATH=value
Isy89
  • 179
  • 8
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/29965143) – Philip Wrage Sep 30 '21 at 21:53