Is there any way to use gcloud with python3?

2020-05-25 06:00发布

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?)

5条回答
淡お忘
2楼-- · 2020-05-25 06:18

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.

查看更多
孤傲高冷的网名
3楼-- · 2020-05-25 06:22

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.

查看更多
放我归山
4楼-- · 2020-05-25 06:24

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.

查看更多
何必那么认真
5楼-- · 2020-05-25 06:28

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
查看更多
劫难
6楼-- · 2020-05-25 06:32

The system requirement explicitly said python 2.7.x https://cloud.google.com/sdk/downloads

why do you want to run gcloud with python3 anyway?

查看更多
登录 后发表回答