When attempting to run the GCP command line tool gcloud
on MacOS with Python 3.12 the following error occurs:
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'
Any attempt to run gcloud on MacOS with Python 3.12 being used as the default python interpreter on the system's PATH results in the above error. The same error does not occur on Linux or Windows as gcloud bundles its own python interpreter on those platforms.
Installing gcloud's supported python versions (3.8 & 3.9) as the system's default python interpreter solves the issue.
If multiple python interpreters are available, setting the
CLOUDSDK_PYTHON
environment variable to point to the path of the preferred interpreter will solve the issue. More information is available on the gcloud CLI's MacOS installation page: https://cloud.google.com/sdk/docs/install#mac