Is there any way to use gcloud with python3?

24,622

Solution 1

I worked around this issue by specifying the path to Python 2 (that I named python2 on my system).

$ export CLOUDSDK_PYTHON=$(which python2)
$ ./install.sh

I suggest adding the export to your .bashrc or .zshrc file.

Solution 2

As of 2019-12-17, version 274.0.0 officially supports Python 3. Release notes:

Cloud SDK now has GA support for Python 3. Please run gcloud topic startup for:

  • Information on configuring the Python interpreter used by the Cloud SDK.
  • List of tools in the Cloud SDK that still require a Python 2.7 interpreter.
  • List of known issues with Python 3 support.

(That command shows that dev_appserver and endpointscfg are the exceptions.)

According to the search order, gcloud will still use Python 2 if it finds it. You can be explicit by setting CLOUDSDK_PYTHON=python3 (or similar) as an environment variable.

Solution 3

gcloud-python and gcloud-cli as in Cloud SDK are somewhat unrelated products. It is true that you need python 2.7.x to run gcloud-cli, but that does no preclude you from using python3 with gcloud-python library.

If you install multiple versions of python 2.7x and 3.5 for example (you can even make python3 default) as long as you set CLOUDSDK_PYTHON environment variable to point to python 2.7.x interpreter you should be able to run gcloud-cli while using python3 for your project.

On Windows for example, Cloud SDK packages its own python which does not conflict with any other version you might have on your system. It is pure runtime dependency for gcloud-cli.

Solution 4

Inside the install.sh, it says python3 is supported but not prioritised because python 2 is, I think, more ubiquitous. It means if you are running macOS, add a line of environment variable by echo "export CLOUDSDK_PYTHON=/your/path/to/python3" >> ~/.bash_profile will allow gcloud to use whichever python3 is located.

If it doesn't work, then point it to whichever python 2 and only use python 3 for your own work should solve the problem.

# if CLOUDSDK_PYTHON is empty
if [ -z "$CLOUDSDK_PYTHON" ]; then
  # if python2 exists then plain python may point to a version != 2
  if _cloudsdk_which python2 >/dev/null; then
    CLOUDSDK_PYTHON=python2
  elif _cloudsdk_which python2.7 >/dev/null; then
    # this is what some OS X versions call their built-in Python
    CLOUDSDK_PYTHON=python2.7
  elif _cloudsdk_which python >/dev/null; then
    # Use unversioned python if it exists.
    CLOUDSDK_PYTHON=python
  elif _cloudsdk_which python3 >/dev/null; then
    # We support python3, but only want to default to it if nothing else is
    # found.
    CLOUDSDK_PYTHON=python3
  else
    # This won't work because it wasn't found above, but at this point this
    # is our best guess for the error message.
    CLOUDSDK_PYTHON=python
  fi
fi
Share:
24,622
piper
Author by

piper

Updated on December 20, 2020

Comments

  • piper
    piper over 3 years

    I have a little confused about gcloud with python3

    After I installed gcloud in python3 env and I tried to example Quickstart for Python in the App Engine Flexible Environment.

    It said 'You need Google Cloud SDK', so I installed SDK. All the process after SDK(including SDK), It needs python2 env.

    Here is a question, Is is impossible to run gcloud with python3 (officially ) yet? (SDK and python2 with gcloud library is best way?)

  • piper
    piper almost 8 years
    Thank you for answering, I thought If I want to use GCP, I should install gcloud library and SDK. The bottom of the gcloud library page, there is a python3. So, I think there's some othere reason why python 3 is there which I don't know. But, If I install SDK, There's no reason to install pip install --upgrade gcloud right? :)
  • marcadian
    marcadian almost 8 years
    The gcloud library seems is a connector, you don't really need to use that if you don't want. It also depends which part of GCP you're using, if for example use appengine, you can skip gcloud SDK and use appengine SDK. Gcloud SDK itself has more features as it's also used for GCE, GKE, etc
  • piper
    piper almost 8 years
    Thank your for your answer, I get the concept 'gcloud' is just connector that I can use in my python script to control GCP env.
  • cherba
    cherba over 6 years
    After you install, when you run gcloud it actually invokes which python2 under the hood. So if above worked you do not need to set this environment variable in your .bashrc. On the other hand if python2 is not on a path, then you should explicitly set env variable to its full path.
  • Kedare
    Kedare over 6 years
    My reasons would be that it's my last tool still requiring legacy Python :) Everything else is already running on Python 3
  • Manza
    Manza over 5 years
    @marcadian because all new releases will in python 3, will not have sense to be on the latest and more stable version of python?, why I dont undestand is why google dont have this supported yet
  • headwinds
    headwinds almost 3 years
    thank you @mmla & @cherba! First, I was install gcloud with your tip. Then I tried to update my gcloud components and it complained again about "python3 cannot be opened because the developer cannot be verified." so I added export CLOUDSDK_PYTHON=$(which python2) to the end of my .zshrc and it worked again!
  • Ian
    Ian almost 3 years
    Confused, this doesn't help with python3 ? Works if you have python2 and python3 installed I guess. JCottons is more correct though.