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
importlibmode; don't rely on sibling test modules becoming importable automatically (mode tradeoffs). - Tests intentionally import each other: choose a package tree with
__init__.pyfiles; keep standalone test module names unique (package layout). - One path is generated/build output: choose
--ignore=PATHto 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 flag | Value / default | Applies to | Source |
|---|---|---|---|
--import-mode | importlib derives unique names; default prepend; standalone test names must be unique in prepend/append modes | Current pytest; importlib added in 6.0 | modes |
addopts | In pytest.toml, under [pytest], use ["--import-mode=importlib"] and retain any other intended options | pytest 9.2.0; TOML format added in 9.0 | good practices, config formats |
testpaths | List directories to search for tests; set to the actual intended test roots | Current configuration | reference |
--ignore=PATH | Ignore a path during collection; can be repeated | Current collection CLI | reference |
--collect-only | Collect and display test nodes without executing tests | Current collection CLI | reference |
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) | Cause | Fix | Source |
|---|
| 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