Getting error ImportMismatchError while running py.test Getting error ImportMismatchError while running py.test docker docker

Getting error ImportMismatchError while running py.test


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.


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


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.