简体   繁体   中英

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.

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/

When running my tests with tox in a virtualenv

You don't really need to run tox in a virtualenvironment. Tox creates a virtualenv to install your project into. Automation tools typically supply standardised tools for a fresh python executable.

Sometimes after refactoring a project; changing the project folder structure for example, causes this. The best remedy usually is simply calling:

tox --recreate

I always do this in my CI/CD. Otherwise it is possible that that caches are used, this can cause trouble for the idempotency of those builds.

If this doesn't work, check whether your build agent cleans up the build directory properly. It might leave remnants from other builds.

This would achieve the same as deleting the .tox folder, which you could also consider.

EDIT : I've cloned your project, it has nothing to do with either pytest or tox im afraid. The structure of your project (imports) is rather broken.

If you apply the following rules you should be improving:

  • Use only absolute imports (and stick with it < PEP8 advice)
  • Don't add imports in the init if you do not need them
  • Make imports as specific as possible (explicit better than implicit < zen of python)

Finally, there is an import loop happening somewhere (eg file1 import file2, file2 import file1). But i'll let you find that one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM