Artifact Registry に Python パッケージを保存する

このクイックスタートでは、プライベート Artifact Registry Python リポジトリを設定し、パッケージをアップロードして、パッケージをインストールする方法を示します。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  9. Make sure that you have the following role or roles on the project: Artifact Registry Administrator

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      IAM に移動
    2. プロジェクトを選択します。
    3. [ アクセスを許可] をクリックします。
    4. [新しいプリンシパル] フィールドに、ユーザー ID を入力します。 これは通常、Google アカウントのメールアドレスです。

    5. [ロールを選択] リストでロールを選択します。
    6. 追加のロールを付与するには、 [別のロールを追加] をクリックして各ロールを追加します。
    7. [保存] をクリックします。
  10. Cloud Shell を起動する

    このクイックスタートでは、Google Cloudでホストされているリソースを管理するためのシェル環境である Cloud Shell を使用します。

    Cloud Shell には、Google Cloud CLI と Python がプリインストールされています。gcloud CLI は、 Google Cloudの主要なコマンドライン インターフェースを提供します。

    Cloud Shell を起動します。

    1. Google Cloud コンソールに移動します。

      Google Cloud コンソール

    2. Google Cloud コンソールのツールバーで、「Cloud Shell をアクティブにする」をクリックします:

    コンソールの下部にあるフレーム内で Cloud Shell セッションが開きます。このシェルで gcloud コマンドを実行します。

    必要なパッケージをインストールする

    Twine は、Python パッケージを公開するためのツールです。Twine を使用して、パッケージを Artifact Registry にアップロードします。

    このクイックスタートでは、Cloud Shell に含まれている Python インストールを使用します。このデフォルトのインストールには、Artifact Registry で認証を処理するための Artifact Registry キーリング バックエンドが含まれています。仮想環境を作成するか、Cloud Shell の外部で Python を設定する場合は、認証用の keyring バックエンドをインストールする必要があります。詳細については、キーリングで認証するをご覧ください。

    Twine をインストールするには、次のコマンドを実行します。

    pip install twine
    

    これで、Artifact Registry を設定する準備が整いました。

    リポジトリを作成する

    パッケージのリポジトリを作成します。

    1. 次のコマンドを実行して、現在のプロジェクトでロケーション us-central1quickstart-python-repo いう新しい Python パッケージ リポジトリを作成します。

      gcloud artifacts repositories create quickstart-python-repo \
          --repository-format=python \
          --location=us-central1 \
          --description="Python package repository"
      
    2. 次のコマンドを実行して、リポジトリが作成されたことを確認します。

      gcloud artifacts repositories list
      
    3. gcloud コマンドを簡素化するには、デフォルトのリポジトリを quickstart-python-repo に、デフォルトのロケーションを us-central1 に設定します。値を設定した後は、リポジトリまたはロケーションを必要とする gcloud コマンドでそれらを指定する必要はありません。

      リポジトリを設定するには、次のコマンドを実行します。

      gcloud config set artifacts/repository quickstart-python-repo
      

      ロケーションを設定するには、次のコマンドを実行します。

      gcloud config set artifacts/location us-central1
      

      これらのコマンドの詳細については、gcloud config set のドキュメントをご覧ください。

    認証を構成する

    Artifact Registry キーリング バックエンドは、環境内で認証情報を探す戦略であるアプリケーションのデフォルト認証情報(ADC)を使用して認証情報を検索します。

    このクイックスタートでは、次のことを行います。

    • ADC のユーザー認証情報を生成します。本番環境では、サービス アカウントを使用し、GOOGLE_APPLICATION_CREDENTIALS 環境変数で認証情報を指定する必要があります。
    • pip コマンドと twine コマンドに Artifact Registry リポジトリ URL を含めると、リポジトリ URL を使用して pip と Twine を構成する必要がなくなります。

    ADC の認証情報を生成するには、次のコマンドを実行します。

    gcloud auth application-default login
    

    認証方法と、pip と Twine の構成にリポジトリを追加する方法については、Python パッケージ リポジトリへの認証を設定するをご覧ください。

    サンプル パッケージを取得する

    Python プロジェクトをビルドすると、ディストリビューション ファイルは Python プロジェクトの dist サブディレクトリに保存されます。このクイックスタートを簡素化するため、ビルド済みのパッケージ ファイルをダウンロードします。

    1. python-quickstart という名前の Python プロジェクト フォルダを作成します。

      mkdir python-quickstart
      
    2. dist という名前のサブディレクトリを作成し、そのディレクトリに変更します。

      mkdir python-quickstart/dist
      cd python-quickstart/dist
      
    3. Python Packaging User Guide チュートリアル Packaging Python Projects で使用されているサンプル Python パッケージをダウンロードします。

      pip download sampleproject
      

      このコマンドは、sampleproject パッケージとその依存関係である peppercorn をダウンロードします。

    パッケージをリポジトリにアップロードする

    Twine を使用して、パッケージをリポジトリにアップロードします。

    1. dist ディレクトリから、親の python-quickstart ディレクトリに移動します。

      cd ..
      
    2. dist ディレクトリからリポジトリにパッケージをアップロードします。

      python3 -m twine upload --repository-url https://us-central1-python.pkg.dev/PROJECT_ID/quickstart-python-repo/ dist/*
      

      python3 -m を使用してコマンドを実行すると、Python は Twine を見つけてコマンドを実行します。twine コマンドがシステムパスにある場合は、python3 -m なしで実行できます。

    Twine は sampleprojectpeppercorn の両方をリポジトリにアップロードします。

    リポジトリ内のパッケージを表示する

    パッケージが追加されたことを確認するには、quickstart-python-repo リポジトリ内のパッケージを一覧表示します。

    次のコマンドを実行します。

    gcloud artifacts packages list --repository=quickstart-python-repo
    

    パッケージのバージョンを表示するには、次のコマンドを実行します。

    gcloud artifacts versions list --package=PACKAGE
    

    PACKAGE はパッケージ ID です。

    パッケージをインストールする

    次のコマンドを実行してパッケージをインストールします。

    pip install --index-url https://us-central1-python.pkg.dev/PROJECT_ID/quickstart-python-repo/simple/ sampleproject
    

    トラブルシューティング

    デフォルトでは、pip や Twine などのツールは詳細なエラー メッセージを返しません。エラーが発生した場合は、--verbose フラグを指定してコマンドを再実行し、詳細な出力を取得します。詳細については、Python パッケージのトラブルシューティングをご覧ください。

    クリーンアップ

    このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。

    リポジトリを削除する前に、保持する必要があるパッケージが別の場所で使用可能であることを確認してください。

    リポジトリを削除するには、次の手順を行います。

    1. quickstart-python-repo リポジトリを削除するには、次のコマンドを実行します。

      gcloud artifacts repositories delete quickstart-python-repo
      
    2. アクティブな gcloud 構成のデフォルトのリポジトリとロケーション設定を削除するには、次のコマンドを実行します。

      gcloud config unset artifacts/repository
      gcloud config unset artifacts/location
      

    次のステップ