在 Artifact Registry 中存储 Python 软件包
本快速入门介绍如何设置私有 Artifact Registry Python 代码库、上传软件包,然后安装该软件包。
准备工作
- 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.
- 
    
    
      
        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 theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      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 theserviceusage.services.enablepermission. Learn how to grant roles.
- 
    
        Make sure that you have the following role or roles on the project: Artifact Registry Administrator Check for the roles- 
              In the Google Cloud console, go to the IAM page. Go to IAM
- Select the project.
- 
              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. 
- 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- 
              In the Google Cloud console, go to the IAM page. 前往 IAM
- 选择项目。
- 点击 授予访问权限。
- 
              在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。 
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
 
- 
              
- 
    
    
      
        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 theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      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 theserviceusage.services.enablepermission. Learn how to grant roles.
- 
    
        Make sure that you have the following role or roles on the project: Artifact Registry Administrator Check for the roles- 
              In the Google Cloud console, go to the IAM page. Go to IAM
- Select the project.
- 
              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. 
- 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- 
              In the Google Cloud console, go to the IAM page. 前往 IAM
- 选择项目。
- 点击 授予访问权限。
- 
              在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。 
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
 
- 
              
- 前往 Google Cloud 控制台。 
- 在 Google Cloud 控制台工具栏上,点击激活 Cloud Shell:  
- 运行以下命令,在位置 - us-central1中名为- quickstart-python-repo的当前项目中创建一个新的 Python 软件包代码库。- gcloud artifacts repositories create quickstart-python-repo \ --repository-format=python \ --location=us-central1 \ --description="Python package repository"
- 运行以下命令确认已创建代码库: - gcloud artifacts repositories list
- 为了简化 - 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 文档。 
- 为 ADC 生成用户凭据。在生产环境中,您应该使用服务账号,并通过 GOOGLE_APPLICATION_CREDENTIALS环境变量提供凭据。
- 在 pip和twine命令中添加 Artifact Registry 代码库网址,这样您就无需使用代码库网址来配置 pip 和 Twine。
- 创建一个名为 - python-quickstart的 Python 项目文件夹。- mkdir python-quickstart
- 创建一个名为 - dist的子目录,然后切换到该目录。- mkdir python-quickstart/dist cd python-quickstart/dist
- 下载 Python 封装用户指南教程封装 Python 项目中使用的示例 Python 软件包。 - pip download sampleproject- 该命令会下载 - sampleproject软件包及其依赖项- peppercorn。
- 从 - dist目录更改为父级- python-quickstart目录。- cd ..
- 将软件包从 - 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即可运行该命令。
- 如需删除 - quickstart-python-repo代码库,请运行以下命令:- gcloud artifacts repositories delete quickstart-python-repo
- 如果要移除您为活跃 - gcloud配置配置的默认代码库和位置设置,请运行以下命令:- gcloud config unset artifacts/repository gcloud config unset artifacts/location
- 详细了解如何配置身份验证
- 了解如何管理代码库
- 了解如何管理软件包
- 阅读我们关于 DevOps 的资源,并了解我们的研究项目。
启动 Cloud Shell
在本快速入门中,您将使用 Cloud Shell,这是一个用于管理Google Cloud上托管资源的 Shell 环境。
Cloud Shell 预安装有 Google Cloud CLI 和 Python。gcloud CLI 为 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 了。
创建代码库
为软件包创建代码库。
配置身份验证
Artifact Registry 密钥环后端使用应用默认凭据 (ADC) 查找您的凭据,这是一种在您的环境中查找凭据的策略。
在本快速入门中,您将执行以下操作:
如需为 ADC 生成凭据,请运行以下命令:
gcloud auth application-default login
如需详细了解身份验证方法以及如何将代码库添加到 pip 和 Twine 配置中,请参阅为 Python 软件包代码库设置身份验证。
获取示例软件包
构建 Python 项目时,分发文件会保存在 Python 项目的 dist 子目录中。为简化本快速入门,您将下载预构建的软件包文件。
将软件包上传到代码库
使用 Twine 将软件包上传到您的代码库。
Twine 会将 sampleproject 和 peppercorn 都上传到您的代码库。
查看代码库中的软件包
如需验证您的软件包是否已添加,请列出 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 账号产生费用,请按照以下步骤操作。
在移除代码库之前,请确保已在其他位置保存了您要保留的任何软件包。
如需删除代码库,请执行以下操作: