NathanLooney
NathanLooney

Reputation: 651

gcloud: ModuleNotFoundError: No module named 'imp'

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.

Upvotes: 50

Views: 32440

Answers (12)

Onur Manap
Onur Manap

Reputation: 1

I had the same issue with python 3.12. You can first install 3.11 by doing:

brew install python@3.11

End then:

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

Worked for me, hope also works for you!

Upvotes: 0

Shanti Guzm&#225;n
Shanti Guzm&#225;n

Reputation: 1

Solution for Windows!

If you are a Windows user and have Python3.12 installed you can follow the next steps:

DOWNLOAD "Google cloud SDK for Windows-bundled with Python"

Verify you have the bundled python in your C:\

GO to C:\SWDTOOLS\google-cloud-sdk-356.0.0-windows\platform

Here you should see the folder \bundledpython\ with python.exe

MODIFY your CLOUDSDK_PYTHON and point to the C:\SWDTOOLS\google-cloud-sdk-356.0.0-windows\platform\bundledpython\python.exe

Then you can continue without the imp error

Upvotes: 0

Dentrax
Dentrax

Reputation: 1267

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 python3.11 if you don't have: brew install python@3.11)
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

Upvotes: 58

einnocent
einnocent

Reputation: 3984

tl;dr

Copy/paste the following commands into your Terminal

brew install python@3.11
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 python@3.11

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.

Upvotes: 27

Prashant Sengar
Prashant Sengar

Reputation: 636

I had installed gcloud SDK by downloading the binaries and setting the path in my zsh profile. The issue occured for me because I installed pyenv which updated my default python path.

To fix this, I set the CLOUDSDK_PYTHON in my environment to the original /usr/bin/python3 for which it was working earlier.

Upvotes: 1

Seven
Seven

Reputation: 3262

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.*.

Upvotes: 1

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.

Upvotes: 0

BigLeo
BigLeo

Reputation: 7809

If you use MacOS you can fix it by:

brew upgrade google-cloud-sdk

Then

gcloud components update

Upvotes: 24

Kyle H
Kyle H

Reputation: 3303

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.

Upvotes: 18

Eric Liu
Eric Liu

Reputation: 79

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

Upvotes: 6

Mehdi.t
Mehdi.t

Reputation: 39

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

Upvotes: 3

NathanLooney
NathanLooney

Reputation: 651

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

Upvotes: 9

Related Questions