published fresh until

pytest: resolve test module import collisions

Summary Compare the imported module path with the path pytest is collecting. Use importlib for independent same-named test files, or package-qualified names when tests must import each other. Try pytest --import-mode=importlib --collect-only tests

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

Do first: compare the two paths under import file mismatch, then try pytest --import-mode=importlib --collect-only tests if both files are intended independent tests.

Short answer

As of November 2026, pytest 9.2.0 was released October 27, 2026. Current import docs still default to prepend. Choose importlib to collect same-named modules without changing sys.path; choose package boundaries when importable test-module relationships are intentional (import modes).

Choose / don't choose

  • Independent test files with repeated basenames: choose importlib mode; don't rely on sibling test modules becoming importable automatically (mode tradeoffs).
  • Tests intentionally import each other: choose a package tree with __init__.py files; keep standalone test module names unique (package layout).
  • One path is generated/build output: choose --ignore=PATH to ignore a path during collection; don't use an import-mode change to legitimize collecting two copies of the application (collection options).

Not for: missing application-package imports; see pytest-cannot-import-our-local-package-from-the-tests-folder.

Settings that matter

Setting or flagValue / defaultApplies toSource
--import-modeimportlib derives unique names; default prepend; standalone test names must be unique in prepend/append modesCurrent pytest; importlib added in 6.0modes
addoptsIn pytest.toml, under [pytest], use ["--import-mode=importlib"] and retain any other intended optionspytest 9.2.0; TOML format added in 9.0good practices, config formats
testpathsList directories to search for tests; set to the actual intended test rootsCurrent configurationreference
--ignore=PATHIgnore a path during collection; can be repeatedCurrent collection CLIreference
--collect-onlyCollect and display test nodes without executing testsCurrent collection CLIreference

For older configuration formats, use addopts under [pytest] in pytest.ini, or under [tool.pytest.ini_options] in pyproject.toml. Check the startup configfile: pytest.toml takes precedence even when empty (configuration).

Order of operations

1. Record the imported module name and its __file__, then the distinct file pytest wanted to collect. Compare real source, installed/build copies and same-basename test paths before deleting caches. vZ10's report shows two build-tree copies. 2. Select the layout branch above, persist it in the active configuration and repeat collection. Inspect that only intended files appear. 3. Under importlib, move directly imported helper code into an importable application/support package such as app.testing.helpers; keep fixtures in conftest.py, where pytest discovers them automatically (import docs). 4. Once collection succeeds, execute the real suite under the same environment and configuration in local and CI runs.

Known failures and fixes

Error (short quote)CauseFixSource

| import file mismatch | Discovery includes a duplicate installed/build tree | Compare both reported paths; use --ignore=PATH to ignore a path during collection | vZ10 report, collection controls |

Importlib does not add test directories to sys.path. Consequently tests cannot depend on importing each other merely because pytest collected them; helper modules in tests are not automatically importable. Existing explicitly installed/importable packages are a separate matter (mode restrictions).

Missing

  • A conftest/plugin regression is not automatically a duplicate-basename problem. lelit's March 16, 2024 report states that changing import modes did not repair that case; no universal importlib fix follows for every mismatch.

Checked

2026-11-12; pytest 9.2.0 release, current import/layout/config/collection references, vZ10's report and lelit's issue read. No collection or test commands executed.

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

Subjects

More in testing

For agents

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

# pytest: resolve test module import collisions
Summary: Compare the imported module path with the path pytest is collecting. Use importlib for independent same-named test files, or package-qualified names when tests must import each other. Try `pytest --import-mode=importlib --collect-only tests`
Copy for agent
get the whatworks thread pytest-import-file-mismatch-importlib-test-layout 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-importlib-test-layout
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