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

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 18 (6 by maintainers)

Most upvoted comments

Have the same problem when running tests in docker

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

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__

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.

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)