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" error (__pycache__) when project directory is accessible via symlinks #3607

Closed
nyanpasu64 opened this issue Jun 21, 2018 · 18 comments
Labels
type: bug problem that needs to be addressed

Comments

@nyanpasu64
Copy link

nyanpasu64 commented Jun 21, 2018

For cloud sync reasons, I symlinked C:\Users\nyanpasu64\encrypted as an alias for nyanpasu64\Dropbox\encrypted (the actual folder).

I have a virtualenv under C:\Users\nyanpasu64\[Dropbox\]encrypted\code\amk_tools\venv.

C:\Users\nyanpasu64\encrypted\code\amk_tools\venv\Scripts (symlink) is present in PATH.

I have a setup.cfg file under code\amk_tools, containing

[tool:pytest]
testpaths = tests

[coverage:run]
branch = True
source =
    amktools

I'm using both PyCharm test runner, and the integrated terminal, to run tests.

When I run pytest from cmd in pwd= C:\Users\nyanpasu64\Dropbox\encrypted\code\amk_tools, it works fine. When I run pytest from cmd in pwd= C:\Users\nyanpasu64\encrypted\code\amk_tools, I get the following error:

import file mismatch:
imported module 'tests.test_mmkparser' has this __file__ attribute:
  C:\Users\nyanpasu64\Dropbox\encrypted\code\amk_tools\tests\test_mmkparser.py
which is not the same as the test file we want to collect:
  C:\Users\nyanpasu64\encrypted\code\amk_tools\tests\test_mmkparser.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

and one such error per test_*.py file.

Removing all __pycache__ files and running pytest from encrypted, it works and now Dropbox\encrypted\ has the error.

pip list:

amktools (0.0.0, c:\users\nyanpasu64\dropbox\encrypted\code\amk_tools)
atomicwrites (1.1.5)
attrs (18.1.0)
click (6.7)
colorama (0.3.9)
coverage (4.5.1)
more-itertools (4.1.0)
parsimonious (0.8.0)
pip (9.0.1)
pluggy (0.6.0)
plumbum (1.6.6)
py (1.5.3)
pytest (3.6.2)
pytest-cov (2.5.1)
pytest-pycharm (0.5.0)
ruamel.yaml (0.15.37)
setuptools (28.8.0)
sf2utils (0.9.0)
six (1.11.0)

pytest (3.6.2) on Windows 10 x64 with developer mode and symlinks enabled

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #774 ("import file mismatch" with git), #2078 (Error when --confcutdir is not a directory), #702 ("import file mismatch" despite different names (only Windows 7?)), #200 (disable the creation of the pycache directory), and #223 (pytest fails collecting on py25 with "import file mismatch" - HINT: remove pycache / .pyc files and/or use a unique basename for your test file modules).

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jun 21, 2018
@pbecotte
Copy link

Have the same problem when running tests in docker

@blueyed
Copy link
Contributor

blueyed commented Aug 30, 2018

@pbecotte
Most likely not the same I guess (not related to symlinks).

In general, see #2042.

@blueyed
Copy link
Contributor

blueyed commented Aug 30, 2018

Should we close this as duplicate?

@nyanpasu64
Copy link
Author

Dunno, my issue does involve symlinks

@pbecotte
Copy link

Not a symlink issuer per-se, but Docker allows you to mount your source-code into a container as a volume- meaning that if you run pytest inside a container the pycache files point to a path that does not exist on the host (meaning that you cannot run pytest from the host without cleaning up the output files)

@blueyed
Copy link
Contributor

blueyed commented Aug 30, 2018

@pbecotte
That's what I thought.. I use this alias py-clean=find -name "*.pyc" -delete for this.

@jimbo1qaz
I would assume that fixing #2042 would get rid of the error also in your case.

@pbecotte
Copy link

Yes, I added that to my Makefile to workaround the issue, just figured I would add the comment for anyone else who was seeing the same problem. Thanks!

@nicoddemus
Copy link
Member

@jimbo1qaz can you confirm if deleting the .pyc files fixes the problem?

@nyanpasu64
Copy link
Author

Yes, that's how I worked around it (though it was rather annoying to manually perform that whenever switching between IDE and CLI tests).

@nicoddemus
Copy link
Member

Thanks for reporting back. 👍

I'm closing this because this (long standing) issue is already tracked by #2042.

@marcstreeter
Copy link

In my case I just needed to read the error a little more --

my tests directory content structure was like so

├── README.md
├── foo
    ├── test_foo.py
    ├── test_bar.py
    └── ...
├── bar
    ├── test_foo.py
    ├── test_bar.py
    └── ...

I had to change it so that the sub test modules weren't the same as such

├── README.md
├── foo
    ├── test_foo_foo.py
    ├── test_foo_bar.py
    └── ...
├── bar
    ├── test_bar_foo.py
    ├── test_bar_bar.py
    └── ...

I didn't have to have those exact naming scheme, I only needed to make sure that my test module files were named uniquely.

@squarewave24
Copy link

squarewave24 commented Apr 4, 2019

Have the same problem when running tests in docker

in my case with Docker, it was the __pycache__

since i run tests before dockerizing.

removing __pycache__ fixed it

@oldnote
Copy link

oldnote commented May 22, 2019

#3607 (comment)

In my case I just needed to read the error a little more --

my tests directory content structure was like so

...

I had to change it so that the sub test modules weren't the same as such

...

I didn't have to have those exact naming scheme, I only needed to make sure that my test module files were named uniquely.

Fixed same issue by adding __init__.py in each tests directory instead of renaming tests.

@ArnaudPel
Copy link

ArnaudPel commented May 24, 2019

Side note on the 'docker' occurrence of that problem: I fixed it by adding the following two lines in .dockerignore:

*.pyc
**/__pycache__

@ffffaaasl
Copy link

Python

@ffffaaasl
Copy link

@walexhenrique
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests