A J
A J

Reputation: 3754

Getting error ImportMismatchError while running py.test

When I am running tests locally its working fine, but after creating the docker and running inside the container I am getting below error.

    /usr/local/lib/python3.5/site-packages/_pytest/config.py:325: in _getconftestmodules
    return self._path2confmods[path]
E   KeyError: local('/apis/db/tests')

During handling of the above exception, another exception occurred:
/usr/local/lib/python3.5/site-packages/_pytest/config.py:356: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/apis/db/tests/conftest.py')

During handling of the above exception, another exception occurred:
/usr/local/lib/python3.5/site-packages/_pytest/config.py:362: in _importconftest
    mod = conftestpath.pyimport()
/usr/local/lib/python3.5/site-packages/py/_path/local.py:680: in pyimport
    raise self.ImportMismatchError(modname, modfile, self)

_pytest.config.ConftestImportFailure: ImportMismatchError('conftest', '/projects/my_project/db/tests/conftest.py', local('/apis/db/tests/conftest.py'))

/apis - its the WORKDIR in Dockerfile.

Upvotes: 92

Views: 32195

Answers (10)

Apteryx
Apteryx

Reputation: 6352

In my case, I was getting errors such as:

_ ERROR collecting guix-build-python-pyfakefs-5.7.2.drv-0/pyfakefs-5.7.2/pyfakefs/tests/fake_tempfile_test.py _
import file mismatch:
imported module 'pyfakefs.tests.fake_tempfile_test' has this __file__ attribute:
  /gnu/store/x8ijgilj2knh0qsnzmr4xyk14gk74ayz-python-pyfakefs-5.7.2/lib/python3.10/site-packages/pyfakefs/tests/fake_tempfile_test.py
which is not the same as the test file we want to collect:
  /tmp/guix-build-python-pyfakefs-5.7.2.drv-0/pyfakefs-5.7.2/pyfakefs/tests/fake_tempfile_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules

While attempting to run the pyfakefs test suite. It turns out this was caused by the package installing its test files (and making them available on the Python path), which were conflicting with the local source files discovered by Pytest. I could tell Pytest to prefer the freshly installed copy by using the --import-mode=importlib argument:

pytest --import-mode=importlib

Upvotes: 0

nocdib
nocdib

Reputation: 1426

I had this same problem but for me it wasn't about pycache. My issue was that I was running pytest from a directory that contained a cloned git repo of a custom pytest plugin and a folder with the test files that made use of that plugin.

The solution was to move the cloned repo folder outside of the scope of pytest execution which is the current directory or subdirectory of the from where the command is executed.

Upvotes: 0

Thiagarajan Hariharan
Thiagarajan Hariharan

Reputation: 466

If you are running py.test FILE from directory ROOT, ensure that FILE is somewhere under ROOT. If it's ELSEWHERE, pytest may try to load plugins from ELSEWHERE and report ImportMismatchError. That was the cause in my case. Copying FILE to under ROOT fixed the problem.

Upvotes: 1

jlhasson
jlhasson

Reputation: 2286

In my case I was setting PYTHONPATH manually to the root of my repo. Inside of this I have a worktrees/ folder, which contains worktrees, each of which contain a python package. Since I told python to look for packages at the root of my repo, it would consider all packages under worktrees/ as well, thus leading to the import error. I resolved by flattening this nested structure.

Upvotes: 0

Hari_pb
Hari_pb

Reputation: 7416

Found __pycache__ files in coverage/fullcoverage/ which are hidden in jupyter notebook etc.

simply navigate to the folder and use rm -r __pyache__/ . This will take care of your pytest.

Upvotes: 3

monitorius
monitorius

Reputation: 3956

You can set environment variable PY_IGNORE_IMPORTMISMATCH=1 to skip this errros. It should be fine in simple cases like running tests inside and outside docker container.

Upvotes: 20

A J
A J

Reputation: 3754

I have fixed it by removing all __pycache__ pkg under test/ directory, the issue was when I was creating docker image its picking all my __pycache__ and *.pyc files too, at the time when test are running its using my local machine path instead of the path in docker container.

Conclusion: Clear your *.pyc and __pycache__ files before creating a docker image.

Upvotes: 153

Abhilash
Abhilash

Reputation: 291

Delete all the .pyc files. You can do this by find . -name \*.pyc -delete

Upvotes: 17

amit kumar
amit kumar

Reputation: 21022

I am using Python 3.6. In my case, I was getting ImportMismatchError in modules with the same name under different packages, e.g., A/B/main.py and C/D/main.py. Python 3 does not require __init__.py file in source folders, but adding __init__.py under A/B and C/D solved the issue.

Upvotes: 28

Datageek
Datageek

Reputation: 26689

You can use the .dockerignore file to exclude all __pycache__ folders from being sent to the docker image context:

.dockerignore file, excludes __pycache__ folders and *.pyc files from all sub/folders:

**/__pycache__
**/*.pyc

Upvotes: 39

Related Questions