できない.dev

pytest で「import file mismatch」が出て同じ名前のテストファイルを収集できない

import file mismatch は、テストファイル名を一意にするか、__init__.py を置くか、importlib モードにすれば直る。
同名のテストファイルが同じモジュール名で衝突すると出る。

公開: 更新:

症状できない · exit 2
pytest
…
collected 1 item / 1 error
==================================== ERRORS ====================================
__________________ ERROR collecting tests/unit/test_utils.py ___________________
解決後できた · exit 0
pytest
…
tests/integration/test_utils.py .                                        [ 50%]
tests/unit/test_utils.py .                                               [100%]
============================== 2 passed in 0.01s ===============================

2026-10-28 に python:3.12(pytest 9.2.0) で実際に打って取った出力。検証環境の詳細

要約

テストの収集中に次のエラーが出て、テストが 1 本も実行されずに終わる(終了コード 2)。

__________________ ERROR collecting tests/unit/test_utils.py ___________________
import file mismatch:
imported module 'test_utils' has this __file__ attribute:
  /p/tests/integration/test_utils.py
which is not the same as the test file we want to collect:
  /p/tests/unit/test_utils.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

pytest の既定の import 方式 prepend は、__init__.py の無いディレクトリにあるテストファイルを、そのディレクトリを sys.path の先頭に足してからファイル名だけのモジュール名で import する。
tests/integration/test_utils.py と tests/unit/test_utils.py はどちらも test_utils という名前になり、先に import した側が後の側の import の結果として返ってくるので、pytest が「別のファイルだ」と止める。
上の表示は pytest 9.1.1・Python 3.13 で、この 2 ファイルだけを置いたディレクトリで pytest を実行したものだ。

pytest tests/unit のように片方のディレクトリだけを実行すると通るので、全体を回す CI で初めて気付くことが多い。

実行例

python:3.12 のコンテナで、同名の test_utils.py を __init__.py の無い 2 つのディレクトリに置いて pytest 9.2.0 を実行すると、収集で import file mismatch が出て終了コード 2 になった。
__init__.py を置く、--import-mode=importlib を付ける、ファイル名を変えるのいずれでも、2 件とも通って終了コード 0 になった。

$ find tests -name "*.py"
tests/unit/test_utils.py
tests/integration/test_utils.py
$ pytest
============================= test session starts ==============================
platform linux -- Python 3.12.16, pytest-9.2.0, pluggy-1.7.0
rootdir: /tmp/tmp.HR1TTfhmM3
collected 1 item / 1 error
 
==================================== ERRORS ====================================
__________________ ERROR collecting tests/unit/test_utils.py ___________________
import file mismatch:
imported module 'test_utils' has this __file__ attribute:
  /tmp/tmp.HR1TTfhmM3/tests/integration/test_utils.py
which is not the same as the test file we want to collect:
  /tmp/tmp.HR1TTfhmM3/tests/unit/test_utils.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
=========================== short test summary info ============================
ERROR tests/unit/test_utils.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.05s ===============================
$ echo $?
2
$ touch tests/__init__.py tests/unit/__init__.py tests/integration/__init__.py
$ pytest
============================= test session starts ==============================
platform linux -- Python 3.12.16, pytest-9.2.0, pluggy-1.7.0
rootdir: /tmp/tmp.HR1TTfhmM3
collected 2 items
 
tests/integration/test_utils.py .                                        [ 50%]
tests/unit/test_utils.py .                                               [100%]
 
============================== 2 passed in 0.01s ===============================
$ echo $?
0
$ pytest --import-mode=importlib
============================= test session starts ==============================
platform linux -- Python 3.12.16, pytest-9.2.0, pluggy-1.7.0
rootdir: /tmp/tmp.HR1TTfhmM3
collected 2 items
 
tests/integration/test_utils.py .                                        [ 50%]
tests/unit/test_utils.py .                                               [100%]
 
============================== 2 passed in 0.01s ===============================
$ echo $?
0
$ mv tests/integration/test_utils.py tests/integration/test_utils_integration.py
$ pytest
============================= test session starts ==============================
platform linux -- Python 3.12.16, pytest-9.2.0, pluggy-1.7.0
rootdir: /tmp/tmp.HR1TTfhmM3
collected 2 items
 
tests/integration/test_utils_integration.py .                            [ 50%]
tests/unit/test_utils.py .                                               [100%]
 
============================== 2 passed in 0.01s ===============================
$ echo $?
0

— 2026-10-28 時点の出力

検証環境Python 3.12.16 / Git 2.47.3 / pytest 9.2.0 / 2026-10-28 検証
検証日
実行環境
python:3.12 Debian GNU/Linux 13 (trixie)
バージョン
  • Python 3.12.16
  • Git 2.47.3
  • pytest 9.2.0

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. ファイル名を一意にする

もっとも単純な方法で、ほかの設定に影響しない。

mv tests/integration/test_utils.py tests/integration/test_utils_integration.py

2. __init__.py を置いてパッケージにする

tests/
  __init__.py
  unit/
    __init__.py
    test_utils.py
  integration/
    __init__.py
    test_utils.py

モジュール名が tests.unit.test_utils と tests.integration.test_utils のように分かれるので衝突しない。
片方のディレクトリにだけ置いても通ったが、置き忘れで再発しやすいので、テストのディレクトリにはそろえて置く。

3. importlib モードにする

pyproject.toml で import 方式を importlib にすると、pytest は sys.path を変えずに、rootdir からの位置をもとに一意のモジュール名を付けて import する。

[tool.pytest.ini_options]
addopts = "--import-mode=importlib"

Good Integration Practices(新しいタブで開く) は新しいプロジェクトにこのモードを勧めている。
ただし、テストファイル同士で import し合うことはできなくなる。
テストで共有する補助関数は conftest.py のフィクスチャにするか、テストの外のパッケージに置く。

よくある原因

  1. 同じ名前のテストファイルがある。
    単体テストと結合テストのように、ディレクトリを分けて同じ名前のファイルを置いている。
  2. __init__.py が無い。
    テストのディレクトリがパッケージになっていないので、ディレクトリ名がモジュール名に入らない。
  3. import 方式が既定のままになっている。
    ドキュメント(新しいタブで開く)は、テストのディレクトリがパッケージでない場合、テストファイルの名前はほかのテストファイルと重ならないようにする必要があるとしている。

HINT にある __pycache__ の削除は、この構成では効かない。
キャッシュを消してから実行し直しても同じエラーになった。