How to fix "AttributeError: module 'collections' has no attribute 'Mapping'" with Google Cloud SDK package?
Asked Answered
O

1

12

I tried to install Google Cloud SDK on macOS, but it shows the following error.

Can anyone help please?

"Welcome to the Google Cloud SDK!
Traceback (most recent call last):
  File "/Users/kaab/google-cloud-sdk/bin/bootstrapping/install.py", line 12, in <module>
    import bootstrapping
  File "/Users/kaab/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 46, in <module>
    from googlecloudsdk.core.updater import update_manager
  File "/Users/kaab/google-cloud-sdk/lib/googlecloudsdk/core/updater/update_manager.py", line 39, in <module>
    from googlecloudsdk.core.console import progress_tracker
  File "/Users/kaab/google-cloud-sdk/lib/googlecloudsdk/core/console/progress_tracker.py", line 651, in <module>
    class _BaseStagedProgressTracker(collections.Mapping):
AttributeError: module 'collections' has no attribute 'Mapping'"
Oestrogen answered 2/12, 2021 at 7:28 Comment(0)
T
41

Are you running Python 3.10 or newer? Switch to an older Python version, or if you can edit the code, change the import statement to from collections.abc import Mapping.

When I import from collections import Mapping in Python 3.9, I get the message:

<stdin>: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

(ABC stands for abstract base class.)

This means that ever since Python 3.3, the correct way to import this is from collections.abc import Mapping, and the old way finally stopped working in Python 3.10.

I'd call it a bug in Google Cloud SDK, but their documentation (at the time of the question) actually suggests to use Python 3.5 to 3.8, so I suspect they have not tested it on 3.10.

Edit: apparently Cloud SDK now (January 2024) says it supports up to 3.12. Presumably they have fixed the import. This answer might still be relevant to people who face this deprecation issue in other libraries.

Tacye answered 2/12, 2021 at 7:59 Comment(8)
Its same for MutableMapping.Grissel
this happened to me in gcloud version 368, I updated to latest version in macos following the official guide and python 3.10 and the problem doesn't happen, although, they do say to use python <=3.8. cloud.google.com/sdk/docs/install#macWhopper
@khan maybe click the "accept" checkmark on the answer if it helped, if you can. (I don't know if you can, now that the question is closed)Tacye
If you don't want to change your python version, you can modify the gsutil code. First, locate the file which gsutil, mine is ~/google-cloud-sdk/bin/gsutil. Then open that file, change this line CLOUDSDK_PYTHON=$(order_python python3 python2 python2.7 python) to this CLOUDSDK_PYTHON=$(order_python python2 python3 python2.7 python)Sharpeyed
If I understand that correctly, that just changes the priority to use python2 instead of python3, but note that modern MacOS versions do not have a python2 anymore (since version 12.3 Monterey, in 2021), so you would need to install that separately, too.Tacye
The docs now say 3.8 to 3.12.Confounded
But is there still an error? If that's the case, submit a bug report.Tacye
Most likely not, but I don't know.Confounded

© 2022 - 2024 — McMap. All rights reserved.