Nicolas Bouliane

How to fix ImportMismatchError in Python Posted on

If you get an ImportMismatchError when running a Python app, it’s likely that you have some Python bytecode files (*.pyc) from a different runtime. For example, this happens when I run my Python unit tests inside a Docker container, then try to run them again in PyCharm.

Here is an example error message:

/.../env/bin/python2.7 "/Applications/PyCharm" --target
Testing started at 10:51 AM ...
Launching py.test with arguments in /Users/xxxxxxx/Projects/.../app
Traceback (most recent call last):
 File "/.../env/lib/python2.7/site-packages/_pytest/", line 362, in _importconftest
 mod = conftestpath.pyimport()
 File "/.../env/lib/python2.7/site-packages/py/_path/", line 680, in pyimport
 raise self.ImportMismatchError(modname, modfile, self)
ImportMismatchError: ('', '/app/', local('/Users/xxxxxxx/.../'))
ERROR: could not load /Users/xxxxxxx/.../

Process finished with exit code 0
Empty test suite.

The solution to this error is simple: delete all *.pyc files in your project. You can do this with a simple command:

find . -name \*.pyc -delete

Your Python code should now run properly.