13
10

More than 3 years have passed since last update.

Python3でgcloudコマンドを動かせるようにする

Last updated at Posted at 2020-02-05

tl;dr;

export CLOUDSDK_PYTHON=python3

説明

pyenv 等でpython3, gcloudをインストールした環境では、gcloud実行時に以下のようなエラーが出ることがある。

$ pyenv local
3.7.4
$ gcloud
pyenv: python2: command not found
The `python2' command exists in these Python versions:
  2.7.17

gcloudがpython2でしか動作しないのかというとそうではなく、
ドキュメントにはGoogle Cloud SDK version 274.0.0以降であればpython3.5以上で動作する(GA)と書いてある。
https://cloud.google.com/sdk/docs/?hl=en

As of Cloud SDK version 274.0.0, the gcloud CLI has GA support for running using a Python 3.5 and up interpreter. For exclusions and more information on configuring your Python interpreter, run gcloud topic startup.

gcloud topic startup を読んでみると……

NAME
    gcloud topic startup - supplementary help for gcloud startup options

DESCRIPTION
Choosing a Python Interpreter

    The gcloud CLI runs under Python. On Windows, you have the option of
    installing a bundled Python interpreter, but on Mac and Linux you must have
    a Python interpreter available somewhere on your system. gcloud will
    attempt to locate an interpreter on your system PATH by looking for the
    following binaries (in this order):

      ▪ python2
      ▪ python27
      ▪ python
      ▪ python3

    Currently gcloud requires Python version 2.7.x or 3.5 and up.

    Other Python tools shipped in the Cloud SDK do not yet support Python 3,
    including:

      ▪ dev_appserver
      ▪ endpointscfg

    If you have multiple Python interpreters available or if you don't have one
    on your PATH, you can specify which interpreter to use by setting the
    CLOUDSDK_PYTHON environment variable. For example:

        # Use the python3 interpreter on your path
        $ export CLOUDSDK_PYTHON=python3

        # Use a python you have installed in a special location
        $ export CLOUDSDK_PYTHON=/usr/local/my-custom-python-install/python

    gsutil now supports Python 3.5 and up in addition to Python 2.7.x. To use a
    different interpreter for gsutil than for the other Python tools, you can
    set the desired interpreter by setting the CLOUDSDK_GSUTIL_PYTHON
    environment variable.

    bq now supports Python 3.5 and up in addition to Python 2.7.x. To use a
    different interpreter for bq than for the other Python tools, you can set
    the desired interpreter by setting the CLOUDSDK_BQ_PYTHON environment
    variable.

すなわち、

  • python2 にpathが通っていると、python2が優先的に使おうとする。
  • python2 にpathが通っていなければpython3が使われる
  • 環境変数 CLOUDSDK_PYTHON=python3 を指定しておくとpython3が使われる
  • 一部のCloud SDK toolはpython3に未対応
    • dev_appserverendpointscfg

とのことなので、python2とpython3が共存している環境では、 CLOUDSDK_PYTHON=python3 を設定しておくとgcloudがpython3を使ってくれるようになる。

世の中には python2を使わせる設定をする記事はたくさんあるものの、python3を使わせる設定についてはヒットしなかったのでメモ。
- python3がGAになったgcloud 274.0.0のリリース日は2019年12月18日。まだ1ヶ月半くらいだった。

13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10