Closed kylepw closed 5 years ago
Great question! Here is the short answer to your specific question:
mod/
, you can run python -m unittest tests/test_module.py
.mod/tests/
, then running python -m unittest test_module.py
will fail.Here's the longer explanation. The directory structure you described is as follows:
mod/
module.py
tests/
test_module.py (contains "import module")
The usage of the -m
flag for the Python interpreter is something I did not really address in my guide, because it introduces some of its own complications. Below is a quick primer.
Suppose that the terminal is currently at some directory dir/
. Then, the following command starts a Python interpreter with sys.path[0]
set to the empty string ""
which refers to the current path /path/to/dir/
.
/path/to/dir/> python -m <module_name>
Therefore, in case 1 described above (/path/to/mod/> python -m unittest tests/test_module.py
), the mod/
directory is in sys.path
, so test_module.py
is able to successfully call import module
.
However, in case 2 (/path/to/mod/tests/> python -m unittest test_module.py
), mod/
is not in sys.path
, so the call to import module
fails.
Thanks a lot @chrisyeh96 for the thorough response! Makes more sense now.
Great guide! I enjoyed it and learned a lot. I have a question regarding relative imports. You mentioned that modules in parent directories couldn't be imported without messing with sys.path. I was wondering about
unittest
and if it's different? For example say I have a directorymod/
withmodule.py
and a unit-test for that module,test_module.py
, inmod/tests/
, I should be able toimport module
inmod/tests/test_module.py
and runpython -m unittest tests/test_module.py
even though it's importing from the parent directory, correct?