Getting error ImportMismatchError while running py.test

PythonDockerPytest

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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

Solution 5 - Python

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.

Solution 6 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA JView Question on Stackoverflow
Solution 1 - PythonA JView Answer on Stackoverflow
Solution 2 - PythonDatageekView Answer on Stackoverflow
Solution 3 - Pythonamit kumarView Answer on Stackoverflow
Solution 4 - PythonAbhilashView Answer on Stackoverflow
Solution 5 - PythonmonitoriusView Answer on Stackoverflow
Solution 6 - PythonHari_pbView Answer on Stackoverflow