Search code examples
pythonvirtualenvimporterrortox

ImportMismatchError while running tox


When running my tests with tox in a virtualenv I run into py._path.local.LocalPath.ImportMismatchError: ('tfields.__main__', '/builds/dboe/tfields/.tox/py38/lib/python3.8/site-packages/tfields/__main__.py', local('/builds/dboe/tfields/tfields/__main__.py')). I keep coming back to Getting error ImportMismatchError while running py.test but removing pycache and *.pyc does not solve my issue. See https://gitlab.mpcdf.mpg.de/dboe/tfields/-/jobs/1122409 for the failing example of my ci (You can see in lines 96 and 97 that I remove pycache and *.pyc as suggested in the answers to the referred question above). Any hints or sollutions are very welcome.


Solution

  • In your testenv section, you have to set either

    setenv = PY_IGNORE_IMPORTMISMATCH=1
    

    or

    usedevelop = true
    

    You can read more about the problem on the pytest bugtracker, see here https://github.com/pytest-dev/pytest/issues/2042

    While I have not spend a lot of time on this, I am pretty sure it has to do with the naming of your packages.

    The main source folder is called tfields and your package is called tfields. The problem is that now both the installed package and the folder is available for Python under the same namespace.

    When I remember correctly, I had the very same problem for my https://github.com/jugmac00/hibpcli project - and the problem went away, once I put my sourcecode in a src directory, and not longer call the top level folder the same as the package name.

    If you want to dig deeper, I highly recommend the article by Hynek Schlawack on why to use a src layout:

    https://hynek.me/articles/testing-packaging/