hagetak's blog

どうも、はげたかです。

python3 で Google Cloud SDK(gcloud)をインストールする

環境

$ python -V
Python 2.7.16

$ python3 -V
Python 3.7.5

結論

環境変数が用意されているので、いれてあげる

export CLOUDSDK_PYTHON=$(which python3)

インストール(失敗編)

$ curl https://sdk.cloud.google.com | bash
...省略
Welcome to the Google Cloud SDK!
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha1
ERROR:root:code for hash sha224 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha224
ERROR:root:code for hash sha256 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha256
ERROR:root:code for hash sha384 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha384
ERROR:root:code for hash sha512 was not found.
Traceback (most recent call last):
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/usr/local/Cellar/python@2/2.7.16/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type sha512
Traceback (most recent call last):
  File "/Users/your-user/google-cloud-sdk/bin/bootstrapping/install.py", line 12, in <module>
    import bootstrapping
  File "/Users/your-user/google-cloud-sdk/bin/bootstrapping/bootstrapping.py", line 39, in <module>
    from googlecloudsdk.core import config
  File "/Users/your-user/google-cloud-sdk/lib/googlecloudsdk/core/config.py", line 29, in <module>
    from googlecloudsdk.core.util import files as file_utils
  File "/Users/your-user/google-cloud-sdk/lib/googlecloudsdk/core/util/files.py", line 596, in <module>
    class Checksum(object):
  File "/Users/your-user/google-cloud-sdk/lib/googlecloudsdk/core/util/files.py", line 599, in Checksum
    def __init__(self, algorithm=hashlib.sha256):
AttributeError: 'module' object has no attribute 'sha256'

python3 が利用されず、python 2.7 でインストーラーが走っている。

python3 でインストール

環境変数 CLOUDSDK_PYTHON をみてくれているので、この環境変数python3のPATHを与えてあげるとインストールできる。

$ export CLOUDSDK_PYTHON=$(which python3)
$ curl https://sdk.cloud.google.com | bash

$ gcloud --version
zsh: command not found: gcloud

# インストール時に`.zshrc`に変更が加われ、gcloudのPATHが追加されたためシェルを再度呼び出す
$ exec -l $SHELL

$ gcloud --version
Google Cloud SDK 276.0.0
bq 2.0.52
core 2020.01.10
gsutil 4.47