Pytest: "import file mismatch" error (__pycache__) when project directory is accessible via symlinks

Created on 21 Jun 2018  ยท  17Comments  ยท  Source: pytest-dev/pytest

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

bug

Most helpful comment

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

All 17 comments

GitMate.io thinks possibly related issues are https://github.com/pytest-dev/pytest/issues/774 ("import file mismatch" with git), https://github.com/pytest-dev/pytest/issues/2078 (Error when --confcutdir is not a directory), https://github.com/pytest-dev/pytest/issues/702 ("import file mismatch" despite different names (only Windows 7?)), https://github.com/pytest-dev/pytest/issues/200 (disable the creation of the __pycache__ directory), and https://github.com/pytest-dev/pytest/issues/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).

Have the same problem when running tests in docker

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

In general, see https://github.com/pytest-dev/pytest/issues/2042.

Should we close this as duplicate?

Dunno, my issue does involve symlinks

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)

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

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!

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

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

Thanks for reporting back. ๐Ÿ‘

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

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.

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

https://github.com/pytest-dev/pytest/issues/3607#issuecomment-477654026

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.

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

*.pyc
**/__pycache__

Python

Was this page helpful?
0 / 5 - 0 ratings