Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"import file mismatch" with git #774

Closed
spaceone opened this issue Jun 15, 2015 · 18 comments
Closed

"import file mismatch" with git #774

spaceone opened this issue Jun 15, 2015 · 18 comments

Comments

@spaceone
Copy link
Contributor

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.

@nicoddemus
Copy link
Member

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.

@spaceone
Copy link
Contributor Author

uhm, no. I don't mean the pytest source code. I mean my tests/ folder which is in a git repository.

@bubenkoff
Copy link
Member

find -name '*.pyc' gives you nothing?

@nicoddemus
Copy link
Member

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.

@spaceone
Copy link
Contributor Author

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

@nicoddemus
Copy link
Member

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.

@spaceone
Copy link
Contributor Author

They aren't.
httoop/tests → test files
httoop/httoop/.* → code

@nicoddemus
Copy link
Member

Sorry, you're correct.

Do your /httoop/tests directories contain __init__.py files?

@spaceone
Copy link
Contributor Author

Yes, they do. I deleted them now. I will see if the error persists then.

@spaceone
Copy link
Contributor Author

Hm, is still occurs after the removal of all $(find tests/ -name init.py -delete)

@spaceone
Copy link
Contributor Author

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

@The-Compiler
Copy link
Member

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?

@spaceone
Copy link
Contributor Author

oh, yes it is that simple to reproduce :D

@hpk42
Copy link
Contributor

hpk42 commented Jun 16, 2015

Yes, it is a known documented limitation. Insert __init__.py files or use unique basenames for your tests.

@hpk42 hpk42 closed this as completed Jun 16, 2015
ryneeverett added a commit to ryneeverett/mkcodes that referenced this issue Jan 5, 2016
This is required for my particular use case -- running pytest on the
generated code directory -- due to
<pytest-dev/pytest#774>. It ought to be
abstracted out, especially if we ever support non-python code blocks.
@progovoy
Copy link

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

anthrotype pushed a commit to anthrotype/fonttools that referenced this issue Jan 15, 2017
We can't use relative imports any more since the tests are now located outside the package.

I had to add __init__.py to Tests/feaLib/ so that pytest does not get confused by the presence
of two test files with the same basename: i.e. Tests/feaLib/builder_test.py and Tests/feaLib/builder_test.py

pytest-dev/pytest#774
http://stackoverflow.com/questions/12582503/py-test-test-discovery-failure-when-tests-in-different-directories-are-called
anthrotype pushed a commit to anthrotype/fonttools that referenced this issue Jan 16, 2017
We can't use relative imports any more since the tests are now located outside the package.

I had to add __init__.py to Tests/feaLib/ so that pytest does not get confused by the presence
of two test files with the same basename: i.e. Tests/feaLib/builder_test.py and Tests/feaLib/builder_test.py

pytest-dev/pytest#774
http://stackoverflow.com/questions/12582503/py-test-test-discovery-failure-when-tests-in-different-directories-are-called
@BouchaaraAdil
Copy link

BouchaaraAdil commented Mar 31, 2017

define an empty pytest.ini file in project tree, or add inside it :

[testenv] 
changedir=tests
deps=-rrequirements/tests.txt
commands=py.test

Frost pushed a commit to Karspexet/Karspexet that referenced this issue Apr 24, 2017
Py.test apparently has some weird behavior with test files that have the same
names in different directories: pytest-dev/pytest#774

Adding empty `__init__.py` files in those directories seems to fix the problem.
Frost pushed a commit to Karspexet/Karspexet that referenced this issue Apr 24, 2017
Py.test apparently has some weird behavior with test files that have the same
names in different directories: pytest-dev/pytest#774

Adding empty `__init__.py` files in those directories seems to fix the problem.
Frost pushed a commit to Karspexet/Karspexet that referenced this issue Apr 24, 2017
Py.test apparently has some weird behavior with test files that have the same
names in different directories: pytest-dev/pytest#774

Adding empty `__init__.py` files in those directories seems to fix the problem.
Frost pushed a commit to Karspexet/Karspexet that referenced this issue Apr 27, 2017
Py.test apparently has some weird behavior with test files that have the same
names in different directories: pytest-dev/pytest#774

Adding empty `__init__.py` files in those directories seems to fix the problem.
@duhaime
Copy link

duhaime commented Feb 7, 2018

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...

@llazzaro
Copy link

llazzaro commented May 7, 2021

In my case, I had to delete the directory builds.

in the line:

is_same = _is_same(str(path), module_file)

module_file was using the builds directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants