Jammy python installation corrupted for just one user: cannot import name 'Mapping' from 'collections'

I keep my Jammy Ubuntu always up to date, with all updates installed almost daily. So everything I have installed is at its latest version.

I have installed two important applications: a Windows VM on libvirt + qemu, and docker (including docker compose).

One day, I noticed that the Virtual Machine Manager won’t start anymore from gnome (I pinned it to my application bar). It just closes automatically, and the error it throws is:
ImportError: cannot import name ‘Mapping’ from ‘collections’ (/usr/lib/python3.10/collections/init.py)

From command-line, the application, however, starts.

But then, I had to run a docker compose command (which had successfully completed the day before), which resulted in this:

docker-compose up -d
Traceback (most recent call last):
  File "/usr/bin/docker-compose", line 33, in <module>
    sys.exit(load_entry_point('docker-compose==1.29.2', 'console_scripts', 'docker-compose')())
  File "/usr/bin/docker-compose", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/usr/lib/python3.10/importlib/metadata/__init__.py", line 171, in load
    module = import_module(match.group('module'))
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 13, in <module>
    import docker.errors
  File "/home/myUserName/.local/lib/python3.10/site-packages/docker/__init__.py", line 1, in <module>
    from .api import APIClient
  File "/home/myUserName/.local/lib/python3.10/site-packages/docker/api/__init__.py", line 1, in <module>
    from .client import APIClient
  File "/home/myUserName/.local/lib/python3.10/site-packages/docker/api/client.py", line 6, in <module>
    import requests
  File "/home/myUserName/.local/lib/python3.10/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/home/myUserName/.local/lib/python3.10/site-packages/urllib3/__init__.py", line 8, in <module>
    from .connectionpool import (
  File "/home/myUserName/.local/lib/python3.10/site-packages/urllib3/connectionpool.py", line 29, in <module>
    from .connection import (
  File "/home/myUserName/.local/lib/python3.10/site-packages/urllib3/connection.py", line 39, in <module>
    from .util.ssl_ import (
  File "/home/myUserName/.local/lib/python3.10/site-packages/urllib3/util/__init__.py", line 3, in <module>
    from .connection import is_connection_dropped
  File "/home/myUserName/.local/lib/python3.10/site-packages/urllib3/util/connection.py", line 3, in <module>
    from .wait import wait_for_read
  File "/home/myUserName/.local/lib/python3.10/site-packages/urllib3/util/wait.py", line 1, in <module>
    from .selectors import (
  File "/home/myUserName/.local/lib/python3.10/site-packages/urllib3/util/selectors.py", line 14, in <module>
    from collections import namedtuple, Mapping
ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)

I have tried to re-install python, worked with chatGPT to try to resolve the issue, but nothing worked.

One thing I tried, was to run the same command as root. Surprise, as root, the same command works without a problem!

So there must be something related to my user settings that does not allow python to run.

However, there are no python folders in my ~
Nothing in my .config related to python, and just a .local/lib/python3.10/site-packages/ out of which I deleted all .pyc files, and a .local/bin/ that has several python files (I didn’t delete those).

So, I tried all I could think of, re-installed python, pip, and now, I’m turning to you if you have any ideas, please help

My python version is 3.10.12

Asked By: Diana P

||

You need to remove the conflicting version of Python dependencies, as suggested in the comments:

rm -rf /home/myUserName/.local/lib/python3.10/

It is conflicting with packages already present in your system installation:

  • docker/__init__.py should be provided by python3-docker . Installing another version of that Python package locally is like having two conflicting versions of Docker, risking more bugs
  • requests/__init__.py should be provided by python3-requests
  • urllib3/__init__.py should be provided by python3-urllib3
  • Python 3.10 removed "’Mapping’ from ‘collections’as planned by the:1: DeprecationWarning: Using or importing the ABCs from ‘collections’ instead of from ‘collections.abc’ is deprecated since Python 3.3, and in 3.10 it will stop working` in previous versions. This is why it’s important to use the version of urllib3 bundled with Ubuntu and not install local versions
  • Removing ~/.local/lib/python3.10/ is always safe. It only contains locally installed Python dependencies installed in an way not recommended. If there were dependencies that were important, you would have used lockfiles and venv or something else

I also recommend that you upgrade from Docker Compose V1 docker-compose to Docker Compose V2 docker compose because V1 is no longer supported: https://docs.docker.com/compose/migrate/ . Using Docker Compose V2 will also avoid this problem in the future because it does not use Python.

Answered By: Daniel T
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.