When you use different external tools in the macOS terminal, you'll eventually run into conflicting dependencies. This is the case when you install Python 3.12 and then try to use gcloud.

This morning, I was greeted with the following error message:

Traceback (most recent call last):
  File "/Users/<username>/google-cloud-sdk-1/lib/gcloud.py", line 137, in <module>
    main()
  File "/Users/<username>/google-cloud-sdk-1/lib/gcloud.py", line 90, in main
    from googlecloudsdk.core.util import encoding
  File "/Users/wujames/test-python/google-cloud-sdk-1/lib/googlecloudsdk/__init__.py", line 23, in <module>
    from googlecloudsdk.core.util import importing
  File "/Users/<username>/google-cloud-sdk-1/lib/googlecloudsdk/core/util/importing.py", line 23, in <module>
    import imp
ModuleNotFoundError: No module named 'imp'

After some research, I found that the issue was related to the Python version. I recently installed Python 3.12 and that was causing the issue. The solution was to update the gcloud SDK to use the latest Python version.

That however wasn't so easy as the gcloud tool itself didn't want to start anymore. Thanks to StackOverflow, I was able to find a solution. Here's what I did:

CLOUDSDK_PYTHON=/opt/homebrew/bin/python3.11 gcloud components update

This command tells the gcloud SDK to use the Python 3.11 version. After running this command, the gcloud tool started to update itself to a version that does support Python 3.12.

After the update, I was able to use the gcloud tool again without any issues. I hope this helps you if you run into the same issue.