Fix ModuleNotFoundError when running pytest
Check the selected interpreter and missing module first, then make the project importable through installation or an intentional import-path configuration.
Start with the missing name in the traceback. It may be your project package, a dependency, or a module imported by a test plugin. Adding tests/__init__.py is not a general fix for all three.
Check the environment
python -c "import sys; print(sys.executable)"
python -m pip --version
python -m pytest --version
Install the project's declared test dependencies into that interpreter. python -m pytest uses the chosen Python and adds the current directory to the normal import path; the standalone pytest command can come from a different environment.
Install the project when it is a package
From a project with valid packaging configuration:
python -m pip install -e .
python -m pytest
An editable installation makes the configured project importable while reflecting supported source edits. It does not necessarily copy the source tree into site-packages. Package discovery, dependencies, and the build backend still need correct configuration. Testing a built wheel in a clean environment is useful when you need to verify the distribution itself.
Configure a source path deliberately
For a repository that intentionally runs against its source tree, pytest 7+ supports:
[pytest]
pythonpath = src
Use . for a suitable flat layout instead of src. These paths are relative to pytest's rootdir, and are added for the test session. rootdir itself does not automatically modify sys.path; it primarily anchors configuration and test identities. Merely selecting a different root does not install the project.
Avoid scattering sys.path.append(".") through tests. That silently depends on the working directory and can mask packaging errors or import the wrong package.
Use tests/init.py for a package layout
tests/
├── __init__.py
└── test_module_functions.py
This makes tests a regular package and changes how the default import modes identify test modules. It can resolve collisions between identically named test files or enable package-relative imports when the surrounding layout supports them. Pytest also supports non-package test directories; an __init__.py is not universally required.
The prepend, append, and importlib import modes have different path behavior. Choose one that matches the project instead of adding files until an accidental import succeeds.
FAQ
Why does a script import successfully while pytest fails?
Direct script execution normally adds the script directory to the import path. Module execution, test collection, and installed entry points can use different paths or interpreters. Compare those details rather than assuming the package is installed everywhere.
What should I use for src/myapp?
A configured editable install is convenient for development. An explicit pythonpath = src is an alternative for source-tree tests. Keep a clean installation test if packaging correctness matters.
Will running from the repository root always fix it?
No. It can make a flat source package visible through python -m pytest, but it cannot supply a missing third-party dependency or correct a broken package configuration.
References: pytest import modes, pytest root directory and configuration, Local project installation.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan