在 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.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Artifact Registry API.

    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. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  7. Make sure that billing is enabled for your Google Cloud project.

  8. Enable the Artifact Registry API.

    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. 新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。

    5. 选择角色列表中,选择一个角色。
    6. 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
    7. 点击 Save(保存)。

启动 Cloud Shell

在本快速入门中,您将使用 Cloud Shell,这是一个用于管理Google 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 会话。您可以使用此 shell 运行 gcloud 命令。

安装必需的软件包

Twine 是一款用于发布 Python 软件包的工具。您将使用 Twine 将软件包上传到 Artifact Registry。

在本快速入门中,您将使用 Cloud Shell 随附的 Python 安装。此默认安装包含 Artifact Registry 密钥环后端:用于处理与 Artifact Registry 的身份验证。如果您创建虚拟环境或在 Cloud Shell 之外设置 Python,则必须安装用于身份验证的 keyring 后端。 如需了解详情,请参阅使用 keyring 进行身份验证

如需安装 Twine,请运行以下命令:

pip install twine

现在,您可以设置 Artifact Registry 了。

创建代码库

为软件包创建代码库。

  1. 运行以下命令,在位置 us-central1 中名为 quickstart-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 环境变量提供凭据。
  • piptwine 命令中添加 Artifact Registry 代码库网址,这样您就无需使用代码库网址配置 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 封装用户指南教程封装 Python 项目中使用的示例 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
    

后续步骤