10

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?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
sfgroups
  • 18,151
  • 28
  • 132
  • 204

3 Answers3

15

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
blhsing
  • 91,368
  • 6
  • 71
  • 106
1

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
Hassan Raza
  • 3,025
  • 22
  • 35
0

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.

Christopher
  • 427
  • 1
  • 8
  • 18
  • It probably only worked because your colab/notebook was using a Python version < 3.9. From [As of 2021, which python version is Google Colab using](https://stackoverflow.com/a/65912526/2745495): "*Note that as of April 2020, Colab uses Python 3.6.9 which should run everything without any errors.*" The moment Google Colab allows / switches to 3.9, it will also probably break. – Gino Mempin Jun 05 '22 at 02:45