gcloud: ModuleNotFoundError: No module named 'imp'

8.6k views Asked by At

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.

9

There are 9 answers

0
NathanLooney On

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

0
Mehdi.t On

Support for imp was removed in Python 3.12 (the warning was added in 3.11 apparently). See: https://docs.python.org/3/whatsnew/3.12.html

3
Eric Liu On

Solution

Updating gcloud to version 452.0.1 or greater resolved the issue for me without downgrading from Python 3.12.

gcloud components update

After updating, I was able successfully run gcloud commands:

$ gcloud components list

Your current Google Cloud CLI version is: 458.0.1
The latest available version is: 458.0.1

...

$ python --version
Python 3.12.1

References

5
Major Major On

For anyone running into this issue, I was able to get around it by setting env variable CLOUDSDK_PYTHON to my Python 3.10 executable. 3.12 did not work still, I assume anything less than 3.12 will work.

1
Dentrax On

Upgrading gcloud to latest version 446.0.0 with brew upgrade google-cloud-sdk didn't resolve the issue for me. So running gcloud components update was still failing with No module named 'imp' error.

  1. Upgrade your google-cloud-sdk first:
brew upgrade google-cloud-sdk
  1. To resolve this issue, you need to have at least python3.11 interpreter on your environment. (You will need to install it if you don't have.)
export CLOUDSDK_PYTHON=$(which python3.11)
  1. Upgrade the components: (Press y to continue)
gcloud components update
  1. Then you can able to use gcloud without any issues using python3.12:
unset CLOUDSDK_PYTHON
gcloud version
1
BigLeo On

If you use MacOS you can fix it by:

brew upgrade google-cloud-sdk

Then

gcloud components update
0
Fábio Nicolau de Lima On

The version of Google SKD I was using was not compatible with Python 3.12. So I set the CLOUDSDK_PYTHON variable to python3.10, updated the SDK with gcloud components update and then set the CLOUDSDK_PYTHON variable back to python3.12.

Everything is running normally now.

0
Seven On

I had a working gcloud cli installation but a MacOS upgrade might have updated the Python version and thus gcloud cli started failing with the mentioned error.

I followed the official instructions to install gcloud cli, replacing the google-cloud-sdk folder I already had. The updated sdk works fine with Mac's default Python 3.12.*.

0
einnocent On

tl;dr

Copy/pasta into your Terminal

brew install [email protected]
export CLOUDSDK_PYTHON=/opt/homebrew/bin/python3.11
gcloud components update

Detailed Answer

This answer is for those who have Homebrew installed but did not install gcloud via Homebrew (though it should work even if you did). If you did brew upgrade google-cloud-sdk and got

Error: Cask 'google-cloud-sdk' is not installed.

Do

brew install [email protected]

it will either install it, upgrade it, or leave it alone, but the point is you'll have it.

Then do

export CLOUDSDK_PYTHON=/opt/homebrew/bin/python3.11

right there in the Terminal, no need to mess with .zshrc. You only need it for the next command.

Then do

gcloud components update

which should now run because you have an old Python for it.

After the update, you no longer need that CLOUDSDK_PYTHON reference, because the new gcloud runs with the latest Python (for now).

Go ahead and open up a new Terminal window and do gcloud, it should work now.