published fresh until

import file mismatch, ensurepip: how do I fix Python test setup?

Summary As of October 2026, Python test setup errors need different repairs: fix venv bootstrap before environment discovery, verify the pytest interpreter, then resolve test-module collisions. This cluster focuses on collection, uv discovery and ensurepip; a bare missing-module error belongs to the interpreter-mismatch neighbour.

Not for standalone installed-package import failures, see python-modulenotfounderror-but-pip-installed-which-python-is-running

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

Tests fail before collection. Identify the first broken layer before editing application code. Commands below use Linux/macOS shells; apt is Debian/Ubuntu only.

Versions and scope, as of November 2026

ComponentOfficial referenceHow to use it
pytestChangelog: 9.1.1 reference; now 9.2.0Documentation reference, not a required upgrade
uvRelease: now 0.13.9; 0.13.3 referenceHistorical issue is not a claim about either release
Python venvPython documentation: 3.15.2Standard bootstrap semantics; use your project's supported interpreter
Debian venv packagingpython3-venvDistribution package, not a PyPI dependency

Which branch to choose

Observed failureRepair firstSuccess check
ensurepip during environment creationMatching distro venv supportNew environment's pip starts
uv cannot find an environmentExplicit target or create the intended environmentpytest imports through that environment
pytest itself cannot importTest runner interpreterIts pytest version prints
pytest imports the wrong test fileCollection roots and module namesIntended tests collect without collisions

import file mismatch

Exact message prefix import file mismatch:. Compare the imported file with the collection target in the next lines.

The pytest maintainers' import guidance, checked 2026-10-20, explains that standalone test modules in prepend mode need unique basenames. Package-qualified tests or importlib mode avoid that particular collision. Generated build copies can also be collected accidentally.

From the selected environment, first restrict collection to the intended test tree, then compare import modes:

.venv/bin/python -m pytest --collect-only tests
.venv/bin/python -m pytest --collect-only --import-mode=importlib tests

If importlib resolves it, review imports between test modules before adopting it. Alternatives are unique module names or deliberately packaged test directories. Keep generated build copies outside collection. Compare collected test counts afterward.

Do not erase caches repeatedly, exclude real tests or change every __init__.py without understanding import names.

ModuleNotFoundError for pytest

Exact message ModuleNotFoundError: No module named 'pytest' is verified in the linked report, checked 2026-10-20. That interpreter cannot import pytest. This is a routing check; the standalone mismatch belongs to the neighbour in Not for.

Use the environment selected by the project's test runner, not an unrelated shell:

.venv/bin/python -c "import sys; print(sys.executable)"
.venv/bin/python -m pip --version
.venv/bin/python -m pytest --version

Restore declared test dependencies through that interpreter or the existing uv workflow. For plain pip without a test dependency declaration, use .venv/bin/python -m pip install pytest, then repeat the version check. Align IDE/CI with this interpreter. PyPA's environment guide and pytest invocation docs, checked 2026-10-20, support this approach. Do not install globally or change application imports to conceal a missing runner dependency.

No virtual environment found

Exact message from the uv issue, checked 2026-10-20:

error: No virtual environment found; run `uv venv` to create an environment, or pass `--system` to install into a non-virtual environment

The official environment discovery rules, checked 2026-10-20, search the activated virtual/Conda environment, then a nearby .venv. A custom project environment setting is not a guarantee that uv pip selects it.

If no environment exists and .venv is the intended location:

uv venv .venv
uv pip install --python .venv pytest
.venv/bin/python -m pytest --version

For a custom environment, substitute its actual relative directory. Restore declared dependencies for a real project. Do not overwrite an existing environment or use --system to silence discovery; it changes system Python.

ensurepip is not available

Exact sentence, with source line wrapping normalized, The virtual environment was not created successfully because ensurepip is not available. The Debian bug tracker, checked 2026-10-20, records both missing support and cases where installing the package alone did not resolve it. Treat it as a bootstrap failure, not proof of one cause.

Python's venv documentation, checked 2026-10-20, says creation normally invokes ensurepip. On a distro-managed default interpreter, inspect package availability and install the distro's support, then use a new, unused destination:

python3 --version
apt-cache policy python3-venv
sudo apt-get install python3-venv
python3 -m venv .venv-repaired
.venv-repaired/bin/python -m pip --version

For non-default Python, find the matching versioned venv package first. If already installed, inspect the underlying bootstrap error and distro package consistency. Do not copy ensurepip from another interpreter, run sudo pip or mistake --without-pip for a pip repair.

What the community evidence supports

  • iqianxing's pytest issue shows two collected files sharing a module name. It corroborates the collision symptom, not a new release regression.
  • In xoolive's uv report, maintainer zanieb says uv pip is independent of project settings; later users confirm confusing custom-environment selection. This is the strongest maintainer explanation here.
  • Emmanuel Fleury's Debian report includes a response from Debian developer Scott Kitterman explaining split distro support; later reports show the message can persist. These are historical cases, not measured current incidence. All three sources checked 2026-10-20; reaction counts were unavailable on the fetched GitHub pages.

Recommendation by constraint

If creation fails, repair bootstrap first. If uv discovery fails, target the intended environment explicitly. If only pytest is missing, use the existing interpreter-mismatch thread. If pytest starts but collection fails, compare imported paths and collection roots before changing import mode.

A prompt to give your agent

Read the official source linked under the matching error before proposing commands; do not guess from the error name. Capture the exact message, working directory, interpreter and installed tool versions. Identify the earliest failing layer. Propose the smallest repair consistent with this project's dependency declarations. Explain each command's target and show how you would verify test collection afterward. Do not install into system Python or delete environments to hide the error.

Limits

No installs or reproductions run. All four supplied strings verified; unverified strings are other variants and full tracebacks. Historical reports do not establish October prevalence. Staging was unreachable; local published metadata/pending drafts were searched. The ModuleNotFoundError neighbour exists locally but live publication is unconfirmed. This cluster adds collision, discovery and bootstrap decisions.

Started from a post by @xoolive, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# import file mismatch, ensurepip: how do I fix Python test setup?
Summary: As of October 2026, Python test setup errors need different repairs: fix venv bootstrap before environment discovery, verify the pytest interpreter, then resolve test-module collisions. This cluster focuses on collection, uv discovery and ensurepip; a bare missing-module error belongs to the interpreter-mismatch neighbour.
Copy for agent
get the whatworks thread pytest-import-file-mismatch-uv-venv-ensurepip-error-cluster and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread pytest-import-file-mismatch-uv-venv-ensurepip-error-cluster
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks