ModuleNotFoundError with pytest
Be sure to include .
dot in the $PYTHONPATH
env var.
Use $ python -m site
, or this code fragment to debug such issues:
import pprint
import sys
pprint.pprint(sys.path)
Your question managed to use myproject
at three different levels. At least during debugging you might want to use three distinct names, to reduce possible confusion.
Not sure if this solution was specific to my problem, but I simply add __init__.py
to my tests
folder and that solved the problem.
Solution: use the PYTHONPATH
env. var
PYTHONPATH=. pytest
As mentioned by @J_H, you need to explicitly add the root directory of your project, since pytest
only adds to sys.path
directories where test files are (which is why @Mak2006's answer worked.)
Good practice: use a Makefile or some other automation tool
If you do not want to type that long command all the time, one option is to create a Makefile
in your project's root dir with, e.g., the following:
.PHONY: test
test:
PYTHONPATH=. pytest
Which allows you to simply run:
make test
Another common alternative is to use some standard testing tool, such as tox.