pytest cannot import module while python can
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
This problem usually means pytest and your normal Python invocation are not running with the same import path assumptions. The fix is rarely to hack sys.path; it is usually to make the project layout and test invocation reflect how the package is meant to be installed.
Why Python and pytest Can Disagree
When you run a file directly, Python seeds sys.path from the script location and the current environment. pytest also imports test modules, conftest.py, and application code during collection, and its import mode can change how directories are inserted into sys.path.
That is why these two commands can behave differently:
The first runs one file. The second discovers and imports many files from the project tree.
The quickest sanity check is to compare:
Running python -m pytest ensures the same interpreter that can import your package is also the one launching pytest. If one works and the other does not, you likely have an environment or path mismatch rather than a broken module.
The Reliable Fix: Install Your Package and Use a Clear Layout
The most robust setup is a src layout with an editable install:
Then install it into your virtual environment:
A minimal pyproject.toml can look like this:
And your test imports the package the same way production code would:
This avoids the fragile pattern where tests only pass because the current working directory happens to expose source files directly.
Things That Commonly Break Imports
A few patterns cause this issue again and again:
- running
pytestfrom a different interpreter than the one where dependencies are installed - naming a test file the same as a package module
- relying on the repository root being on
sys.path - mixing flat-layout source code with ad hoc relative imports
- missing
__init__.pyin a regular package where one is required
You can inspect what pytest sees with a temporary debugging test:
That is useful for diagnosis, but it should not become the permanent solution.
Use pytest Configuration Carefully
pytest also supports different import modes. In some projects, switching import mode can reduce path surprises:
This can help when test module names clash or when path mutation causes confusing imports. Still, changing the import mode is a secondary fix. A properly packaged project is the primary fix.
If you need test settings, keep them in configuration instead of shell folklore:
That makes test discovery explicit without rewriting import semantics behind the scenes.
Prefer Absolute Imports Inside Tests
Avoid importing application code through relative paths from the test directory. This is brittle:
Use the package name instead:
If that import fails, it is telling you something useful: the package is not installed or not laid out the way production code expects.
Common Pitfalls
The biggest pitfall is fixing the symptom with sys.path.insert(0, "..") inside tests. That can make the suite pass locally while hiding packaging bugs that reappear in CI, IDEs, and deployment tooling.
Another frequent issue is using the global pytest executable while the project dependencies live in a virtual environment. python -m pytest removes that ambiguity.
File naming collisions are also easy to miss. A file named pytest.py, requests.py, or myapp.py can shadow the package you intended to import.
Finally, namespace packages and regular packages behave differently. If you are not intentionally building a namespace package, include __init__.py where appropriate.
Summary
- '
pytestimport failures usually come from path or environment differences, not magic.' - Prefer
python -m pytestso the right interpreter runs the tests. - Package the project properly and install it with
pip install -e .. - Use absolute imports from the package name, not path hacks.
- Treat
sys.pathworkarounds as diagnostics, not as architecture.
Related reading
- Python- What word can have the most consecutive letters removed and still be a dictionary-valid word?
- python-dev installation error ImportError No module named apt_pkg
- Python-Kafka Keep polling topic infinitely
- Python3.6 ImportError cannot import name 'main' Linux RHEL6
- Python how to mock a kafka topic for unit tests?
- Python mock multiple return values
- Python3 ImportError No module named '_ctypes' when using Value from module multiprocessing
- python3 recognizes tensorflow, but doesn''t recognize any of its attributes
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.