I want to run a Python unittest by invoking the
python -m unittest discover
command. Two packages are in in a module as follows
test
|_ mytest.py
my_module
|__foo.py
|__bar.py
where in bar.py
#bar.py
from foo import baz
# Do things using baz
The problem is that when the unittest discover command runs, foo cannot be found in bar.py anymore. This because my_module is seen as a proper module inside the python test, so I would need to do
from my_module.foo import baz
but that wouldn't work when using the module stand-alone. So what to do? I see 2 options, but they are not that neat so I would like more input:
- Let init.py in my_module add my_module to PYTHONPATH
- Let mytest.py add my_module to PYTHONPATH
However, I found this page stating that
Never add a package directory, or any directory inside a package, directly to the Python path.
The arguments put forth seems to make sense, and as our repository grows constantly we should try to get this right.
What would you do?