基本的なことなんだけど、この手のエラーに関する日本語の情報が少なかったので書き残し。
pythonでディレクトリを作るときは __init__.py
が必須。当たり前の話だけど初心者なのでちょっとハマった。
( __init__.py
がなくても動く場合があるからややこしいんだけど)
ない状態だと、同じ構成の名前空間が同一パッケージ内にあると衝突する。
例えば、
root_dir ├ package_name_A │ ├ views.py │ └ tests.py └ package_name_B ├ views.py └ tests.py
を
root_dir ├ package_name_A │ ├ views.py │ └ tests │ └ tests.py └ package_name_B ├ views.py └ tests └ tests.py
にしたいとき、それぞれの tests/
に __init__.py
を置かないと、
____________ ERROR collecting root_dir/package_name_A/tests/tests.py ____________ import file mismatch: imported module 'tests' has this __file__ attribute: C:\Users\user\Desktop\dir\root_dir\package_name_B\tests which is not the same as the test file we want to collect: C:\Users\user\Desktop\dir\root_dir\package_name_A\tests\tests.py HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules generated xml file: C:\Users\user\Desktop\dir\junit.xml
になる。
tests/tests.py
が2つあるので衝突する。
もしこれが
root_dir ├ package_name_A │ ├ views.py │ └ tests.py └ package_name_B ├ views.py └ tests └ tests.py
だったら問題なく動く。
ちょっと不思議。