70

I recently installed python3.10 on my ubuntu system and I believe I made a link from /usr/bin/python3 to /usr/bin/python3.10

If I run python --version I get Python 2.7.17 and if I run python3 --version I get Python 3.10.2

I believe something I did broke something in my global python / pip.

Whenever I try to use pip globally I get this error:

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 7, in <module>
    from pip._internal.cli.main import main
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 22, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 73, in <module>
    vendored("pkg_resources")
  File "/usr/lib/python3/dist-packages/pip/_vendor/__init__.py", line 33, in vendored
    __import__(modulename, globals(), locals(), level=0)
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/__init__.py", line 77, in <module>
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/_vendor/packaging/requirements.py", line 9, in <module>
  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 672, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 632, in _load_backward_compatible
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/extern/__init__.py", line 43, in load_module
  File "/usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/_vendor/pyparsing.py", line 943, in <module>
AttributeError: module 'collections' has no attribute 'MutableMapping'

After googling I thought the issue is that my pip was made using an older version of python I had so I tried to run:

sudo apt remove python-pip python3-pip
sudo apt install python-pip python3-pip

but even after this I still get the same error with pip.

I do have a virtualenv that I was using with a django project (that uses python 3.10) and if I source into that I am able to use the pip there, but I think this just shows the pip in that venv is properly configured

Result of running ls -la /usr/bin/ | grep -i:

brick@nextgearserver:/etc/apache2$ ls -la /usr/bin/ | grep -i "pip\|python"
lrwxrwxrwx  1 root   root          26 Mar 26  2018 dh_pypy -> ../share/dh-python/dh_pypy
-rwxr-xr-x  1 root   root        1056 Apr 16  2018 dh_python2
lrwxrwxrwx  1 root   root          29 Mar 26  2018 dh_python3 -> ../share/dh-python/dh_python3
lrwxrwxrwx  1 root   root          13 Dec  7  2018 lesspipe -> /bin/lesspipe
lrwxrwxrwx  1 root   root          23 Feb 27  2021 pdb2.7 -> ../lib/python2.7/pdb.py
lrwxrwxrwx  1 root   root          24 Jan 15 13:03 pdb3.10 -> ../lib/python3.10/pdb.py
lrwxrwxrwx  1 root   root          23 Dec  8 16:08 pdb3.6 -> ../lib/python3.6/pdb.py
-rwxr-xr-x  1 root   root         292 Apr 30  2021 pip
-rwxr-xr-x  1 root   root         292 Apr 30  2021 pip2
-rwxr-xr-x  1 root   root         293 Apr 30  2021 pip3
lrwxrwxrwx  1 root   root          31 Oct 25  2018 py3versions -> ../share/python3/py3versions.py
lrwxrwxrwx  1 root   root          26 Mar 26  2018 pybuild -> ../share/dh-python/pybuild
lrwxrwxrwx  1 root   root           9 Apr 16  2018 python -> python2.7
lrwxrwxrwx  1 root   root           9 Apr 16  2018 python2 -> python2.7
-rwxr-xr-x  1 root   root     3633000 Feb 27  2021 python2.7
lrwxrwxrwx  1 root   root          33 Feb 27  2021 python2.7-config -> x86_64-linux-gnu-python2.7-config
lrwxrwxrwx  1 root   root          16 Apr 16  2018 python2-config -> python2.7-config
lrwxrwxrwx  1 root   root          19 Jan 30 15:07 python3 -> /usr/bin/python3.10
-rwxr-xr-x  1 root   root     5515256 Jan 15 13:03 python3.10
-rwxr-xr-x  2 root   root     4526456 Dec  8 16:08 python3.6
lrwxrwxrwx  1 root   root          33 Dec  8 16:08 python3.6-config -> x86_64-linux-gnu-python3.6-config
-rwxr-xr-x  2 root   root     4526456 Dec  8 16:08 python3.6m
lrwxrwxrwx  1 root   root          34 Dec  8 16:08 python3.6m-config -> x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx  1 root   root          16 Oct 25  2018 python3-config -> python3.6-config
-rwxr-xr-x  1 root   root         384 Feb  5  2018 python3-futurize
lrwxrwxrwx  1 root   root          10 Oct 25  2018 python3m -> python3.6m
lrwxrwxrwx  1 root   root          17 Oct 25  2018 python3m-config -> python3.6m-config
-rwxr-xr-x  1 root   root         388 Feb  5  2018 python3-pasteurize
-rwxr-xr-x  1 root   root         152 Nov 11  2017 python3-pbr
lrwxrwxrwx  1 root   root          16 Apr 16  2018 python-config -> python2.7-config
lrwxrwxrwx  1 root   root          29 Apr 16  2018 pyversions -> ../share/python/pyversions.py
-rwxr-xr-x  1 root   root        2971 Feb 27  2021 x86_64-linux-gnu-python2.7-config
-rwxr-xr-x  1 root   root        3246 Jan 15 13:03 x86_64-linux-gnu-python3.10-config
lrwxrwxrwx  1 root   root          34 Dec  8 16:08 x86_64-linux-gnu-python3.6-config -> x86_64-linux-gnu-python3.6m-config
-rwxr-xr-x  1 root   root        3283 Dec  8 16:08 x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx  1 root   root          33 Oct 25  2018 x86_64-linux-gnu-python3-config -> x86_64-linux-gnu-python3.6-config
lrwxrwxrwx  1 root   root          34 Oct 25  2018 x86_64-linux-gnu-python3m-config -> x86_64-linux-gnu-python3.6m-config
lrwxrwxrwx  1 root   root          33 Apr 16  2018 x86_64-linux-gnu-python-config -> x86_64-linux-gnu
python2.7-config
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
BRICK MANE
  • 765
  • 1
  • 5
  • 12
  • 2
    Seems like there are still problems with the very recent python release. Therefore I suggest you to use 3.9 instead unless you have a very good reason to use 3.10. Downgrading will probably solve your issue. – BcK Feb 01 '22 at 19:34
  • 5
    solved here https://github.com/pypa/pipenv/issues/5133 Basically, uninstall pipenv from apt and install via pip – YoFlynn Jul 09 '22 at 15:28

15 Answers15

47

In my case, upgrading the following packages worked on Windows 11:

pip install --upgrade pip wheel setuptools requests
Michael M.
  • 10,486
  • 9
  • 18
  • 34
35

The attribute MutableMapping from the module collections got moved into collections.abc in python3.10.

In your case, /usr/share/python-wheels/pkg_resources-0.0.0-py2.py3-none-any.whl/pkg_resources/_vendor/pyparsing.py uses the MutableMapping attribute of collections.

If you can acces the source code, replace all collections.MutableMapping with collections.abc.MutableMapping.

An example :

import collections 
if sys.version_info.major == 3 and sys.version_info.minor >= 10:

    from collections.abc import MutableMapping
else:
    from collections import MutableMapping
Semnodime
  • 1,872
  • 1
  • 15
  • 24
Just Khaithang
  • 1,217
  • 1
  • 14
  • 24
  • 1
    However, this isn't reasonably doable within all 3rd party libraries – OneCricketeer Jul 28 '22 at 07:47
  • Some 3rd party libraries implement this alternative solution ``` try: from collections.abc import Mapping # novm except ImportError: from collections import Mapping ``` – Tom Kelly Sep 01 '22 at 00:41
30

I was getting the same error on ubuntu 22.04, This is how I solved it.

remove pipenv if you have installed it using apt

sudo apt remove pipenv

install pipenv unsing pip

 pip3 install pipenv

activate virtual environment

python3 -m pipenv shell

install from pipfile

pipenv install
Ankit Kumar
  • 389
  • 3
  • 9
9

There are some Libraries aren't fully compatible with 3.10 to the time of writing this answer

You can downgrade to 3.8 or 3.9 for now and it will work seamlessly

you can find all version for python here

choose your most suitable version

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Mohamed Arafa
  • 5,108
  • 1
  • 8
  • 14
  • 1
    I don't think this is accurate. Pip should work out of the box for all Python releases, given it is the defacto Python package manager. Rather, OP is having issues with `/usr/share/python-wheels/pkg_resources` files on the PYTHONPATH. Downgrading would create a separate installation, possibly with new `site_packages`, so this is only a workaround, not really a solution – OneCricketeer Jul 28 '22 at 07:52
9

I'm not sure this qualifies as an "answer", but to offer an additional work-around for the case of a library that relies on the existence of collections.MutableMapping and hasn't been updated for Python 3.10+, you can place the following code directly before the import of the affected library:

import sys

if sys.version_info.major == 3 and sys.version_info.minor >= 10:
    import collections
    setattr(collections, "MutableMapping", collections.abc.MutableMapping)
Jared Sutton
  • 121
  • 1
  • 2
4

For version 3.10 or above –

from collections.abc import MutableMapping

For version 3.9 or lower –

from collections import MutableMapping

If you want this environment completely dynamic then call the below code.

import collections 
if sys.version_info.major == 3 and sys.version_info.minor >= 10:
    from collections.abc import MutableMapping
else:
    from collections import MutableMapping
jak bin
  • 380
  • 4
  • 8
3

just update requests library version to 2.27.1

use :

sudo apt-get install python-requests==2.27.1
Amir
  • 177
  • 2
  • 9
1

In my case pip was trying to install too old pyparsing version from the requirements.txt file. When I changed from 2.0.1 to 2.4.7 everything went fine, so: pip install pyparsing==2.4.7

Tony
  • 7,767
  • 2
  • 22
  • 51
1

We've bumped into this issue (also disguised as ModuleNotFoundError: No module named 'urllib3') with this exemplary stacktrace:

  File "<..>/python3/dist-packages/pipenv/core.py", line 21, in <module>
    import requests
  File "<..>/python3/dist-packages/pipenv/vendor/requests/__init__.py", line 62, in <module>
    from .packages.urllib3.exceptions import DependencyWarning
  File "<..>/python3/dist-packages/pipenv/vendor/requests/packages/__init__.py", line 29, in <module>
    import urllib3

ModuleNotFoundError: No module named 'urllib3'

The solutions posted in a dedicated blog post didn't help. When one actually installs requests or even urllib3 via pip/requirements.txt, the issue mentioned here pops up with this exemplary stacktrace:

  File "<..>/python3/dist-packages/pipenv/core.py", line 21, in <module>
    import requests
  File "<..>/python3/dist-packages/pipenv/vendor/requests/__init__.py", line 65, in <module>
    from . import utils
  File "<..>/python3/dist-packages/pipenv/vendor/requests/utils.py", line 27, in <module>
    from .cookies import RequestsCookieJar, cookiejar_from_dict
  File "/usr/lib/python3/dist-packages/pipenv/vendor/requests/cookies.py", line 172, in <module>
    class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):

AttributeError: module 'collections' has no attribute 'MutableMapping'

What helped in our case was to pin the docker base image we were using to ensure a python 3.8 install/environment (via an ubuntu package, in this case python3-pip). Having left the base image to latest we got a 3.10 python environment, which, as others have mentioned, are not compatible with dependencies that are too old and require 3.8/3.9.

Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
0

An alternative to make python 3 better and more comatible with itself is to use dynamic loading, for instance the code below fails for some versions of python 3.

import tornado.httpclient 

However the following import works (see code below), it uses the fact that python doesn't normally reload modules and that modules can be altered during runtime. It is an advanced type of load patching method to backport elements that should not have been changed in Python 3 in the official repositories but were for political reasons.

import collections.abc
import collections
collections.MutableMapping = collections.abc.MutablesMapping
import tornado.httpclient
user6830669
  • 161
  • 4
0

Downgrade from Python3.10 or higher to Python3.9.

Semnodime
  • 1,872
  • 1
  • 15
  • 24
0

The problem is in the first library that triggers the rest of the others, try *pip uninstall urllib3* or if you need *sudo pip uninstall urllib3* and then *pip uninstall urllib3* which will update the library. Even though it's been a year I hope it helps someone.

Trevisoli
  • 1
  • 2
0

When pip itself is out of date for python3.10 (raising the MutableMapping exception above) and you don't have sudo privileges on the machine in order to install pip from apt (or, as was likely the case for OP, the apt packages hadn't yet been updated), you can use get-pip.py to upgrade pip.

See https://pip.pypa.io/en/stable/installation/#get-pip-py. The other suggested method ensurepip is likely to give the same error as pip.

Download get-pip.py using wget or some other download tool and run python3.10 get-pip.py (or whatever python executable you're working with).

Jake Stevens-Haas
  • 1,186
  • 2
  • 14
  • 26
-1

I wanted to add another solution for this. I was trying to install 3rd party packages to Python3.10 with pip, and I was getting this error while I was trying. I have been tried every single solution, none of them worked for me except this.

codeine
  • 58
  • 6
-2

To fix:

AttributeError: module 'collections' has no attribute 'MutableMapping'

exception, install uplink:

pip install uplink
S.B
  • 13,077
  • 10
  • 22
  • 49
almheb
  • 1