👨‍💻

Python 3.12 利用時、google-cloud-sdk がエラーを出したので最新バージョンにアップデートした

2024/03/09に公開

エラーについて

ターミナルでコマンドを実行していたとき、google-cloud-sdkが以下のようなエラーを出すようになりました。

Traceback (most recent call last):
  File "/Users/tomo/google-cloud-sdk/lib/gcloud.py", line 132, in <module>
    main()
  File "/Users/tomo/google-cloud-sdk/lib/gcloud.py", line 90, in main
    from googlecloudsdk.core.util import encoding
  File "/Users/tomo/google-cloud-sdk/lib/googlecloudsdk/__init__.py", line 23, in <module>
    from googlecloudsdk.core.util import importing
  File "/Users/tomo/google-cloud-sdk/lib/googlecloudsdk/core/util/importing.py", line 23, in <module>
    import imp
ModuleNotFoundError: No module named 'imp'

エラーメッセージを下に調べてみると、以下のURLが見つかりました。
gcloud: ModuleNotFoundError: No module named 'imp'

どうやら、利用しているPythonのバージョンを3.12に変えたため、google-cloud-sdkのバージョンが古いとエラーが出るようです。

google-cloud-sdk をアップデートする

ということで、google-cloud-sdkをアップデートしてみます。
しかし、Python 3.12を利用している状態だと同じエラーが発生します。
まず、Pythonのバージョンを下げる必要がありそうです。

# Python 3.12 を利用している状態でアップデートすると同じエラーが発生する
gcloud components update
Traceback (most recent call last):
  File "/Users/tomo/google-cloud-sdk/lib/gcloud.py", line 132, in <module>
    main()
  File "/Users/tomo/google-cloud-sdk/lib/gcloud.py", line 90, in main
    from googlecloudsdk.core.util import encoding
  File "/Users/tomo/google-cloud-sdk/lib/googlecloudsdk/__init__.py", line 23, in <module>
    from googlecloudsdk.core.util import importing
  File "/Users/tomo/google-cloud-sdk/lib/googlecloudsdk/core/util/importing.py", line 23, in <module>
    import imp
ModuleNotFoundError: No module named 'imp'

asdfを利用しているため、Pythonのバージョンを下げます。
(もし3.11以下のバージョンがインストールされていない場合は、インストールしましょう)

# 現在のバージョン
python -V
Python 3.12.2

# 利用可能なバージョン
asdf list python
  3.10.13
  3.11.6
 *3.12.2

# バージョンの切り替え
asdf global python 3.11.6

バージョンを切り替えたので、アップデートコマンドを実行します。

gcloud components update

すると、アップデートされるコンポーネントの一覧と確認メッセージが表示されます。
Do you want to continue (Y/n)? Yを入力します。

Beginning update. This process may take several minutes.


Your current Google Cloud CLI version is: 410.0.0
You will be upgraded to version: 467.0.0

┌─────────────────────────────────────────────────────────────────────────────┐
│                      These components will be updated.                      │
├─────────────────────────────────────────────────────┬────────────┬──────────┤
│                         Name                        │  Version   │   Size   │
├─────────────────────────────────────────────────────┼────────────┼──────────┤
│ BigQuery Command Line Tool                          │    2.0.101 │  1.6 MiB │
│ BigQuery Command Line Tool (Platform Specific)      │    2.0.101 │  < 1 MiB │
│ Cloud Storage Command Line Tool                     │       5.27 │ 11.3 MiB │
│ Cloud Storage Command Line Tool (Platform Specific) │       5.27 │  < 1 MiB │
│ Google Cloud CLI Core Libraries                     │ 2024.02.29 │ 22.8 MiB │
│ Google Cloud CLI Core Libraries (Platform Specific) │ 2024.01.06 │  < 1 MiB │
│ Google Cloud CRC32C Hash Tool                       │      1.0.0 │  1.2 MiB │
│ anthoscli                                           │     0.2.48 │ 67.0 MiB │
│ gcloud Alpha Commands                               │ 2024.02.29 │  < 1 MiB │
│ gcloud cli dependencies                             │ 2024.02.29 │ 16.6 MiB │
└─────────────────────────────────────────────────────┴────────────┴──────────┘

A lot has changed since your last upgrade.  For the latest full release notes,
please visit:
  https://cloud.google.com/sdk/release_notes

Do you want to continue (Y/n)?  Y

アップデートが始まります。

╔════════════════════════════════════════════════════════════╗
╠═ Creating update staging area                             ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: BigQuery Command Line Tool                 ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: BigQuery Command Line Tool (Platform Sp... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: Cloud Storage Command Line Tool            ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: Cloud Storage Command Line Tool (Platfo... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: Google Cloud CLI Core Libraries            ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: Google Cloud CLI Core Libraries (Platfo... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: Google Cloud CRC32C Hash Tool              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: anthoscli                                  ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: gcloud Alpha Commands                      ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Uninstalling: gcloud cli dependencies                    ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool                   ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: BigQuery Command Line Tool (Platform Spec... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Cloud Storage Command Line Tool (Platform... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Google Cloud CLI Core Libraries              ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Google Cloud CLI Core Libraries (Platform... ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: Google Cloud CRC32C Hash Tool                ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: anthoscli                                    ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud Alpha Commands                        ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Installing: gcloud cli dependencies                      ═╣
╠════════════════════════════════════════════════════════════╣
╠═ Creating backup and activating new installation          ═╣
╚════════════════════════════════════════════════════════════╝

Performing post processing steps...done.

Update done!

To revert your CLI to the previously installed version, you may run:
  $ gcloud components update --version 410.0.0



To take a quick anonymous survey, run:
  $ gcloud survey

アップデートが完了したので、コンポーネントの一覧を表示してみましょう。

gcloud components list

Your current Google Cloud CLI version is: 467.0.0
The latest available version is: 467.0.0

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                    Components                                                   │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────────┬───────────┤
│     Status    │                         Name                         │              ID              │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────────┼───────────┤
│ Not Installed │ App Engine Go Extensions                             │ app-engine-go                │   4.5 MiB │
│ Not Installed │ Appctl                                               │ appctl                       │  18.5 MiB │
│ Not Installed │ Artifact Registry Go Module Package Helper           │ package-go-module            │   < 1 MiB │
│ Not Installed │ Cloud Bigtable Command Line Tool                     │ cbt                          │  15.9 MiB │
│ Not Installed │ Cloud Bigtable Emulator                              │ bigtable                     │   6.8 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator     │  36.2 MiB │
│ Not Installed │ Cloud Firestore Emulator                             │ cloud-firestore-emulator     │  44.4 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator              │  63.3 MiB │
│ Not Installed │ Cloud Run Proxy                                      │ cloud-run-proxy              │  11.3 MiB │
│ Not Installed │ Cloud SQL Proxy                                      │ cloud_sql_proxy              │   7.3 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr        │           │
│ Not Installed │ Kustomize                                            │ kustomize                    │   7.4 MiB │
│ Not Installed │ Log Streaming                                        │ log-streaming                │  11.9 MiB │
│ Not Installed │ Minikube                                             │ minikube                     │  33.6 MiB │
│ Not Installed │ Nomos CLI                                            │ nomos                        │  28.6 MiB │
│ Not Installed │ On-Demand Scanning API extraction helper             │ local-extract                │  13.7 MiB │
│ Not Installed │ Skaffold                                             │ skaffold                     │  23.3 MiB │
│ Not Installed │ Terraform Tools                                      │ terraform-tools              │  63.6 MiB │
│ Not Installed │ anthos-auth                                          │ anthos-auth                  │  20.7 MiB │
│ Not Installed │ config-connector                                     │ config-connector             │  55.6 MiB │
│ Not Installed │ enterprise-certificate-proxy                         │ enterprise-certificate-proxy │   8.3 MiB │
│ Not Installed │ gcloud Beta Commands                                 │ beta                         │   < 1 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java              │ 125.9 MiB │
│ Not Installed │ gcloud app PHP Extensions                            │ app-engine-php               │  21.9 MiB │
│ Not Installed │ gcloud app Python Extensions                         │ app-engine-python            │   8.4 MiB │
│ Not Installed │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras     │  31.5 MiB │
│ Not Installed │ gke-gcloud-auth-plugin                               │ gke-gcloud-auth-plugin       │   7.4 MiB │
│ Not Installed │ kpt                                                  │ kpt                          │  14.4 MiB │
│ Not Installed │ kubectl                                              │ kubectl                      │   < 1 MiB │
│ Not Installed │ kubectl-oidc                                         │ kubectl-oidc                 │  20.7 MiB │
│ Not Installed │ pkg                                                  │ pkg                          │           │
│ Installed     │ BigQuery Command Line Tool                           │ bq                           │   1.6 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                       │  11.3 MiB │
│ Installed     │ Google Cloud CLI Core Libraries                      │ core                         │  22.8 MiB │
│ Installed     │ Google Cloud CRC32C Hash Tool                        │ gcloud-crc32c                │   1.2 MiB │
│ Installed     │ gcloud Alpha Commands                                │ alpha                        │   < 1 MiB │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────────┴───────────┘
To install or remove components at your current SDK version [467.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [467.0.0], run:
  $ gcloud components update

問題なさそうですね。
では、Pythonのバージョンを3.12に戻して確認してみましょう。

asdf global python 3.12.2
gcloud components list

Your current Google Cloud CLI version is: 467.0.0
The latest available version is: 467.0.0

┌─────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                    Components                                                   │
├───────────────┬──────────────────────────────────────────────────────┬──────────────────────────────┬───────────┤
│     Status    │                         Name                         │              ID              │    Size   │
├───────────────┼──────────────────────────────────────────────────────┼──────────────────────────────┼───────────┤
│ Not Installed │ App Engine Go Extensions                             │ app-engine-go                │   4.5 MiB │
│ Not Installed │ Appctl                                               │ appctl                       │  18.5 MiB │
│ Not Installed │ Artifact Registry Go Module Package Helper           │ package-go-module            │   < 1 MiB │
│ Not Installed │ Cloud Bigtable Command Line Tool                     │ cbt                          │  15.9 MiB │
│ Not Installed │ Cloud Bigtable Emulator                              │ bigtable                     │   6.8 MiB │
│ Not Installed │ Cloud Datastore Emulator                             │ cloud-datastore-emulator     │  36.2 MiB │
│ Not Installed │ Cloud Firestore Emulator                             │ cloud-firestore-emulator     │  44.4 MiB │
│ Not Installed │ Cloud Pub/Sub Emulator                               │ pubsub-emulator              │  63.3 MiB │
│ Not Installed │ Cloud Run Proxy                                      │ cloud-run-proxy              │  11.3 MiB │
│ Not Installed │ Cloud SQL Proxy                                      │ cloud_sql_proxy              │   7.3 MiB │
│ Not Installed │ Google Container Registry's Docker credential helper │ docker-credential-gcr        │           │
│ Not Installed │ Kustomize                                            │ kustomize                    │   7.4 MiB │
│ Not Installed │ Log Streaming                                        │ log-streaming                │  11.9 MiB │
│ Not Installed │ Minikube                                             │ minikube                     │  33.6 MiB │
│ Not Installed │ Nomos CLI                                            │ nomos                        │  28.6 MiB │
│ Not Installed │ On-Demand Scanning API extraction helper             │ local-extract                │  13.7 MiB │
│ Not Installed │ Skaffold                                             │ skaffold                     │  23.3 MiB │
│ Not Installed │ Terraform Tools                                      │ terraform-tools              │  63.6 MiB │
│ Not Installed │ anthos-auth                                          │ anthos-auth                  │  20.7 MiB │
│ Not Installed │ config-connector                                     │ config-connector             │  55.6 MiB │
│ Not Installed │ enterprise-certificate-proxy                         │ enterprise-certificate-proxy │   8.3 MiB │
│ Not Installed │ gcloud Beta Commands                                 │ beta                         │   < 1 MiB │
│ Not Installed │ gcloud app Java Extensions                           │ app-engine-java              │ 125.9 MiB │
│ Not Installed │ gcloud app PHP Extensions                            │ app-engine-php               │  21.9 MiB │
│ Not Installed │ gcloud app Python Extensions                         │ app-engine-python            │   8.4 MiB │
│ Not Installed │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras     │  31.5 MiB │
│ Not Installed │ gke-gcloud-auth-plugin                               │ gke-gcloud-auth-plugin       │   7.4 MiB │
│ Not Installed │ kpt                                                  │ kpt                          │  14.4 MiB │
│ Not Installed │ kubectl                                              │ kubectl                      │   < 1 MiB │
│ Not Installed │ kubectl-oidc                                         │ kubectl-oidc                 │  20.7 MiB │
│ Not Installed │ pkg                                                  │ pkg                          │           │
│ Installed     │ BigQuery Command Line Tool                           │ bq                           │   1.6 MiB │
│ Installed     │ Cloud Storage Command Line Tool                      │ gsutil                       │  11.3 MiB │
│ Installed     │ Google Cloud CLI Core Libraries                      │ core                         │  22.8 MiB │
│ Installed     │ Google Cloud CRC32C Hash Tool                        │ gcloud-crc32c                │   1.2 MiB │
│ Installed     │ gcloud Alpha Commands                                │ alpha                        │   < 1 MiB │
└───────────────┴──────────────────────────────────────────────────────┴──────────────────────────────┴───────────┘
To install or remove components at your current SDK version [467.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [467.0.0], run:
  $ gcloud components update

エラーが出なくなりました。
google-cloud-sdkも定期的にバージョンアップしておく必要がありますね。

Discussion