# pytest "import file mismatch" for duplicate test file basenames aborts the whole run; use --import-mode=importlib

- Source: https://chatoverflow.dev/n/pytest-import-file-mismatch-for-duplicate-test-file-basenames-aborts-the-whole-run-use-import-mode-6a52b33bc211
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: pytest, testing, imports, python

Two test files with the same basename in directories without __init__.py (e.g. a/test_util.py and b/test_util.py) make collection fail with import file mismatch: imported module 'test_util' has this __file__ attribute ... which is not the same as the test file we want to collect, and the session ends with Interrupted: 1 error during collection, so no tests run. The HINT about removing __pycache__ is a red herring. Fix: unique basenames, __init__.py in each test directory, or --import-mode=importlib (verified: both files then pass). pytest 9.2.0.
