LoginSignup
3
2

More than 1 year has passed since last update.

Windowsでgcloud コマンド起動するとpythonのエラーになるとき

Posted at

発生したこと

Windows 10 に Cloud SDK をインストールして gcloud components updateを実行すると、Pythonのエラーが表示される。

$ gcloud compnents update
ERROR: gcloud failed to load: module 'collections' has no attribute 'Mapping'
    gcloud_main = _import_gcloud_main()
    import googlecloudsdk.gcloud_main
    from googlecloudsdk.calliope import cli
    from googlecloudsdk.calliope import actions
    from googlecloudsdk.calliope import markdown
    from googlecloudsdk.calliope import usage_text
    from googlecloudsdk.calliope import parser_arguments
    from googlecloudsdk.calliope import parser_completer
    from googlecloudsdk.core.console import progress_tracker
    class _BaseStagedProgressTracker(collections.Mapping):

This usually indicates corruption in your gcloud installation or problems with your Python interpreter.

Please verify that the following is the path to a working Python 2.7 or 3.5+ executable:
    C:\Users\[username]\AppData\Local\Programs\Python\Python310\python.exe

If it is not, please set the CLOUDSDK_PYTHON environment variable to point to a working Python 2.7 or 3.5+ executable.

If you are still experiencing problems, please reinstall the Cloud SDK using the instructions here:
    https://cloud.google.com/sdk/

もうちょっと詳細な状況

  • 環境変数 PATH には C:\Users\[username]\AppData\Local\Programs\Python\Python310 を定義しており、 ターミナル等でのpythonはそちらが起動するようになっている
  • Python 3.8はインストールしていない

対処法

エラーメッセージには表れていないが、環境変数 CLOUDSDK_PYTHONPython3.8のパスを記載する。インストールしていない場合は、Python3.8をインストールして、上記の環境変数にパスをセットすればよい。

https://cloud.google.com/sdk/docs/install?hl=JA に記載があるバージョンのPythonを使用すればよい。

詳細解説

Cloud SDK が要求するPythonのバージョンと、ローカルにあるPythonのバージョンがずれたため、Cloud SDKがエラーを出しています。「デフォルトのPython使ったけど無理だわ! CLOUDSDK_PYTHON に、動きそうなバージョンのPython指定して!(要約)」というのがエラーメッセージの内容です。

というわけで Pythonのダウンロードページ から必要なPythonをインストールして、そのパスを指定したらよいです。

ただ、デフォルトで動作するPythonは最新にしておきたいため、環境変数PATHの書き換えなどは実施していません。Pythonの仮想環境にも頼りたくなかったので、この方法になりました。

そもそも

Cloud SDKにはPythonがバンドルされているので、面倒を避けたかったらそちらを使うほうが楽だと思います。

3
2
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
3
2