ImportError: cannot import name 'MutableMapping' from 'collections'

Question:

I have installed the AWS CLI using pip on my Python 3.9.0a1 alpine Docker image. Installation went fine. When I run the aws command, I’m getting the following error.

aws
Traceback (most recent call last):
  File "/usr/local/bin/aws", line 27, in <module>
    sys.exit(main())
  File "/usr/local/bin/aws", line 23, in main
    return awscli.clidriver.main()
  File "/usr/local/lib/python3.9/site-packages/awscli/clidriver.py", line 68, in main
    driver = create_clidriver()
  File "/usr/local/lib/python3.9/site-packages/awscli/clidriver.py", line 77, in create_clidriver
    load_plugins(session.full_config.get('plugins', {}),
  File "/usr/local/lib/python3.9/site-packages/awscli/plugin.py", line 44, in load_plugins
    modules = _import_plugins(plugin_mapping)
  File "/usr/local/lib/python3.9/site-packages/awscli/plugin.py", line 61, in _import_plugins
    module = __import__(path, fromlist=[module])
  File "/usr/local/lib/python3.9/site-packages/awscli/handlers.py", line 42, in <module>
    from awscli.customizations.history import register_history_mode
  File "/usr/local/lib/python3.9/site-packages/awscli/customizations/history/__init__.py", line 24, in <module>
    from awscli.customizations.history.db import DatabaseConnection
  File "/usr/local/lib/python3.9/site-packages/awscli/customizations/history/db.py", line 19, in <module>
    from collections import MutableMapping
ImportError: cannot import name 'MutableMapping' from 'collections' (/usr/local/lib/python3.9/collections/__init__.py)



python --version
Python 3.9.0a1

Do I need to install any other module to fix this error message?

Asked By: sfgroups

||

Answers:

collections.MutableMapping has been deprecated since Python 3.3, and was officially removed since Python 3.9.

Excerpt from the documentation:

Deprecated since version 3.3, will be removed in version 3.9: Moved
Collections Abstract Base Classes to the collections.abc module.

You can either wait for a Python 3.9-compatible version of awscli to be released, or patch the aws script (under your /usr/local/bin) yourself like this for the time being:

...
import collections
from collections import abc
collections.MutableMapping = abc.MutableMapping
import awscli.clidriver
Answered By: blhsing

I also encountered this error and was not able to get the import work arounds to work.

I did run my work on Google Colab https://colab.research.google.com/ which works a bit like Jupyter Notebook and the code ran fine without having the the MutableMapping issue. I realize that this is not ideal for every user case, but it is a workaround.

Answered By: Christopher

As said in other answers, the issue is the deprecation of some aliases from collections.abc into collections from python 3.10.

If you can’t modify the importations in your scripts because of a third-party import, as a temporary workaround you can do the aliases manually before importing the problematic third-party lib. See the example below for the importation of hyper library for example, which causes similar issues as yours.

import collections

collections.Iterable = collections.abc.Iterable
collections.Mapping = collections.abc.Mapping
collections.MutableSet = collections.abc.MutableSet
collections.MutableMapping = collections.abc.MutableMapping
Answered By: Hassan Raza
Categories: questions 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.