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/ 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/ in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/apis/db/tests/')

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

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

/apis - its the WORKDIR in Dockerfile.

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.

Getting error ImportMismatchError while running py.test, Of those, only when using tox an ImportMismatchError can happen and we know it is not actually an error because tox guarantees for the files to  This should allow for working around cases where py is wrong / getting in the way. While you can (and should) fix the problem when run with Docker (find -name "*.pyc" -delete), there appear to be cases where this is not possible (symlink). The symlink issue might be a bug in py after all (and should get reported/fixed there then).

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:


get rid of "ImportMismatchError" · Issue #2042 · pytest-dev/pytest , lib\site-packages\_pytest\config\ in _importconftest return self. ImportMismatchError(modname, modfile, self) E py. occurred: lib\site-packages​\_pytest\ in from_call result = func() 1 error in 16.39s ```  To create code blocks or other preformatted text, indent by four spaces: This will be displayed in a monospaced font. The first four spaces will be stripped off, but all other whitespace will be preserved.

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

Message 350910, Otherwise, I'm getting the following error in (my currently loved of which I'm Traceback (most recent call last): File "C:\msys64\mingw64\lib\python2.7\site-​packages/_pytest/", line 543, in ImportMismatchError(modname, modfile, self) ImportMismatchError: Then, try running the tests like::. I probably don't encounter it locally because I usually have to run develop before running tests anyway to avoid conflicts with the package installed in system site-packages. I think what it comes down to is that pytest-runner, which is resolving the dependencies using easy_install, doesn't recognize that the package under test is

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/ and C/D/ Python 3 does not require file in source folders, but adding under A/B and C/D solved the issue.

pytest-dev / py / issues / #58, On the other hand, all the tests run correctly using $ py.test MoinMoin . bug report here though, in case of other people get the same issue. ImportError: Python version mismatch: module was compiled for version 3.5, while the interpreter is running version 3.6. Working on a machine with Ubuntu 16.04 and python 3.6. The system has python 2.7, python 3.5 and python 3.6 installed in it.

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.

py.test script raises ImportMismatchError, am running the tests with this command docker-compose run web pytest /​44067609/getting-error-importmismatcherror-while-running-py-test#44104926. Karol. pytest cannot import module while python can. it's get run by the python version defined instead of I had py.test installed globally, while the packages were

mirumee/saleor, variable that suppresses ImportMismatchError exceptions when set to 1 . fix #169, #170: error importing py.log on Windows: no module named syslog . micro-optimizations in py/_code/ for speeding up pytest runs. start passing unicode particular in py2 you might still get problems, though)  Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported.

1.7.0 (2018-10-11) - py, Python test discovery, setup and run of test functions. pyobj_property(name): def get(self): node = self.getparent(getattr(__import__("pytest"), name)) if node is not None: as the import is invalid in py37 and mypy thinks its a error from unittest import IsolatedAsyncioTestCase # type: ImportMismatchError as e: raise self. Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

_pytest.python, Ran into a weird error with coverage, pytest, and Travis today and fix when you get a 'import file mistmatch' error from pip packages you do not control. the tests run using pytest rather than Django's normal test runner. The maintainers of pytest and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.

  • You probably have some problems with python import paths or with symlinks or whatever. What is /projects/my_project and what is /apis? Are they directories or symlinks? How are they related to each other?
  • @MarSoft /projects is in home directory, and /apis is in docker's home directory, btw I have solved the issue. Thanks!
  • You can put these entries in your .dockerignore file so that they are ignored during the build phase. Though they would still be picked up if you bind mount your source folders inside the container, in order to run tests while including local modifications.
  • To remove this issue, I had to locate __pycache__ inside directory of the file, which was reporting this error. Thanks.
  • Unix command: find . \( -name '__pycache__' -or -name '*.pyc' \) -delete
  • For my use-case I also had to add the build folder to the .dockerignore
  • This is essentially a re-write of @AJ's answer. How dies this add value to the OP question?
  • @Tedinoz It's a no-brainer you can copy-paste to fix the problem. Like it or not, many people are just happy with a solution and don't want to understand the background.