Wie man pytest E ModuleNotFoundError: No module named ... behebt
Problem
Beim Ausführen von pytest in Ihrem Projekt sehen Sie die folgende Fehlermeldung:
pytest_import_error.txt
________ ERROR collecting test/test_Algorithms.py ________
ImportError while importing test module '/home/uli/dev/MyModule/test/test_Algorithms.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
test/test_Algorithms.py:3: in <module>
from MyModule.Algorithms import Algorithms
E ModuleNotFoundError: No module named 'MyModule'Lösung
Erstellen Sie innerhalb Ihres tests-Verzeichnisses __init__.py:
create_tests_init.sh
touch tests/__init__.pyIm unwahrscheinlichen Fall, dass das nicht hilft, müssen Sie möglicherweise die Projekt-Wurzel zu Ihrem PYTHONPATH hinzufügen:
run_pytest_with_pythonpath.sh
PYTHONPATH=. pytestsCheck out similar posts by category:
Python, Unit Testing
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow