I often get errors saying "import file mismatch" and a suggestion to remove pycache / *.pyc.
I removed all these files but the message still occurs.
I can think of git being the reason for it because it marks some files as "moved" internally. I don't know what is the underlying magic there.
Are you reusing a previous working copy from Mercurial perhaps? I just tried with a fresh clone and tox runs fine for the main targets.
uhm, no. I don't mean the pytest source code. I mean my tests/ folder which is in a git repository.
find -name '*.pyc' gives you nothing?
Sorry, I thought this was related to our recent move from Hg to Git. Can you post the full error message? There might be a clue in there.
find โ doesn't find any line.
python2.7
==================================================================================================== ERRORS =====================================================================================================
____________________________________________________________________________________ ERROR collecting messaging/test_body.py ____________________________________________________________________________________
import file mismatch:
imported module 'test_body' has this __file__ attribute:
/home/arch/git/httoop/tests/api/test_body.py
which is not the same as the test file we want to collect:
/home/arch/git/httoop/tests/messaging/test_body.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
------------------------------------------------------------------------------- coverage: platform linux2, python 2.7.10-final-0 --------------------------------------------------------------------------------
Coverage HTML written to dir htmlcov
The problem is that your test files are within the package source and have the same name, as the "use a unique basename for your test file modules" message says. I'm not sure there's an easy workaround for that other than moving your test files, as documented here.
They aren't.
httoop/tests โ test files
httoop/httoop/.* โ code
Sorry, you're correct.
Do your /httoop/tests directories contain __init__.py files?
Yes, they do. I deleted them now. I will see if the error persists then.
Hm, is still occurs after the removal of all $(find tests/ -name init.py -delete)
btw. the file is empty.
____________________________________________________________________________________ ERROR collecting messaging/test_body.py ____________________________________________________________________________________
import file mismatch:
imported module 'test_body' has this __file__ attribute:
/home/arch/git/httoop/tests/api/test_body.py
which is not the same as the test file we want to collect:
/home/arch/git/httoop/tests/messaging/test_body.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
------------------------------------------------------------------------------- coverage: platform linux2, python 2.7.10-final-0 --------------------------------------------------------------------------------
Coverage HTML written to dir htmlcov
============================================================================================ short test summary info ================================================================================= 92 passed, 1 xfailed, 1 error in 0.75 seconds =================================================================================
arch@flow-linux: ~/git/httoop/tests
10:17:11(1 J0 !7454 #67) (master) $ file /home/arch/git/httoop/tests/api/test_body.py
/home/arch/git/httoop/tests/api/test_body.py: empty
arch@flow-linux: ~/git/httoop/tests
10:17:22(0 J0 !7455 #68) (master) $ file /home/arch/git/httoop/tests/messaging/test_body.py
/home/arch/git/httoop/tests/messaging/test_body.py: Python script, ASCII text executable
I can reproduce this with test files outside of the package (without __init__.py files), and two test files with the same basename.
It can also be reproduced with this simple structure (with empty files):
.
โโโ bar
โย ย โโโ test_blah.py
โโโ foo
โโโ test_blah.py
It sounds like the "files need unique basenames" thing is just a pytest limitation rather than an actual bug?
oh, yes it is that simple to reproduce :D
Yes, it is a known documented limitation. Insert __init__.py files or use unique basenames for your tests.
Had the same issue.. and adding empty __init__.py files to:
.
โโโ bar
โโโ__init__.py
โ โโโ test_blah.py
โโโ foo
โโโ__init__.py
โโโ test_blah.py
Did not help.
What about:
avoid __init__.py files in your test directories. This way your tests can run easily against an installed version of mypkg, independently from the installed package if it contains the tests or not.
from here:
http://docs.pytest.org/en/latest/goodpractices.html#choosing-a-test-layout-import-rules
define an empty pytest.ini file in project tree, or add inside it :
[testenv]
changedir=tests
deps=-rrequirements/tests.txt
commands=py.test
I hit this because I had a directory structure that looked like this:
โโโ __pycache__
โย ย โโโ app.cpython-36.pyc
โโโ app.py
โโโ tests
โโโ __pycache__
โย ย โโโ test_api.cpython-36-PYTEST.pyc
โโโ test_api.py
I was deleting __pycache__ but needed to delete tests/__pycache__ to get the test runner to start...
Most helpful comment
Yes, it is a known documented limitation. Insert
__init__.pyfiles or use unique basenames for your tests.