How to fix Python tox ImportError: cannot import name 'cmdline' from 'tox'

Problem:

When trying to run your unit tests using tox you see the following error message:

Traceback (most recent call last):
  File "/home/uli/.local/bin/tox", line 5, in <module>
    from tox import cmdline
ImportError: cannot import name 'cmdline' from 'tox' (/usr/local/lib/python3.10/dist-packages/tox/__init__.py)

Solution

You’re using an older version of tox as an executable. To find out which executable you’re using, check this line of the error message:

File "/home/uli/.local/bin/tox", line 5, in <module>

In our case, the executable is

/home/uli/.local/bin/tox

Delete that file!

After that, tox should work fine. If not, update tox using

pip install -U tox