How to fix pytest E ModuleNotFoundError: No module named ...


When running pytest in your project, you see the following error message:

________ ERROR collecting test/ ________
ImportError while importing test module '/home/uli/dev/MyModule/test/'.
Hint: make sure your test modules/packages have valid Python names.
/usr/lib/python3.10/importlib/ in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
test/ in <module>
    from MyModule.Algorithms import Algorithms
E   ModuleNotFoundError: No module named 'MyModule'


Inside your tests directory, create

touch tests/

In the unlikely case that doesn’t help, you might need to add the project root to your PYTHONPATH:

PYTHONPATH=. pytests