arrow_back

Create a Python Artifact Registry and Upload Code

Zyskaj dostęp do ponad 700 modułów i kursów

Create a Python Artifact Registry and Upload Code

Moduł 30 godz. universal_currency_alt 1 punkt show_chart Wprowadzające
info Ten moduł może zawierać narzędzia AI, które ułatwią Ci naukę.
Zyskaj dostęp do ponad 700 modułów i kursów

gem-artifact-registry-python

Google Cloud self-paced labs logo

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Overview

In this lab, you will learn how to create a Python package repository in Artifact Registry and upload a sample Python package. Artifact Registry allows you to manage your private packages and container images in a single location. This lab uses container images for Python packages.

Task 1. Enable the Artifact Registry API

Before you can use Artifact Registry, you need to enable the Artifact Registry API. This task will guide you through the process.

  1. Enable the Artifact Registry API.
gcloud services enable artifactregistry.googleapis.com Note:
This command enables the Artifact Registry API for your project.
  1. Set your Project ID is:
gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
This command sets your active project identity.
  1. Set your default region to
gcloud config set compute/region {{{ project_0.default_region | "REGION" }}} Note:
This command sets your active compute region.

Task 2. Create a Python Package Repository

Now that the API is enabled, you can create a repository to store your Python packages. You will create a repository with the Python package format.

  1. Create a new Python repository in Artifact Registry. Replace and my-python-repo with your desired region and repository name.
gcloud artifacts repositories create my-python-repo \ --repository-format=python \ --location={{{ project_0.default_region | "REGION" }}} \ --description="Python package repository" Note:
This command creates a Python package repository in Artifact Registry.

Task 3. Configure pip to Use Artifact Registry

To upload and install packages from Artifact Registry, you need to configure pip to authenticate with Artifact Registry.

  1. Install the keyrings.google-artifactregistry-auth package.
pip install keyrings.google-artifactregistry-auth Note:
This package provides authentication for pip to access Artifact Registry.
  1. Configure pip to use Artifact Registry. Replace , PROJECT_ID, and my-python-repo with your region, project ID, and repository name.
pip config set global.extra-index-url https://{{{ project_0.default_region | "REGION" }}}-python.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-python-repo/simple Note:
This command configures pip to use your Artifact Registry repository.

Task 4. Create a Sample Python Package

To upload a package, you first need to create one. This task will guide you through creating a simple Python package.

  1. Create a directory for your package.
mkdir my-package cd my-package Note:
This creates a new directory and navigates into it.
  1. Create a setup.py file with the following content.
from setuptools import setup, find_packages setup( name='my_package', version='0.1.0', author='cls', author_email='{{{ user_0.username | "EMAIL" }}}', packages=find_packages(exclude=['tests']), install_requires=[ # List your dependencies here ], description='A sample Python package', ) Note:
This creates a setup file for your Python package.
  1. Create a my_package/__init__.py file (can be empty).
# my_package/__init__.py Note:
This creates an empty initialization file for the package.
  1. Create a my_package/my_module.py file with some sample code.
def hello_world(): return 'Hello, world!' Note:
This creates a sample module within the package.

Task 5. Upload the Package to Artifact Registry

Now that you have a package, you can upload it to Artifact Registry. You will use the twine tool to upload the package.

  1. Install twine.
pip install twine Note:
This installs the Twine package. In CloudShell installations are typically user installations meaning python -m will need to prefix the package module.
  1. Build the package.
python setup.py sdist bdist_wheel Note:
This builds the source distribution and wheel package.
  1. Upload the package to Artifact Registry.
python3 -m twine upload --repository-url https://{{{ project_0.default_region | "REGION" }}}-python.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-python-repo/ dist/* Note:
This command uploads the package to Artifact Registry. You might need to authenticate.

Task 6. Validate Artifact Registry

Confirm the package has been successfully uploaded to artifact registry

  1. Verify the artifact was deployed successfully by navigating to your Artifact Registry repository in the Google Cloud Console or by listing the artifacts using the gcloud CLI.
gcloud artifacts packages list --repository=my-python-repo --location={{{ project_0.default_region | "REGION" }}} Note:
This command lists NPM packages in the Artifact Registry.
  1. Verify that your package is listed in the Artifact Registry repository.
PACKAGE: my-package CREATE_TIME: 2025-06-25T06:06:10 UPDATE_TIME: 2025-06-25T06:06:11 ANNOTATIONS:

Congratulations!

You have successfully created a Python package repository in Artifact Registry, uploaded a sample package, and installed it using pip. Artifact Registry provides a secure and reliable way to manage your Python packages.

Additional Resources

Manual Last Updated Jun 25, 2025

Lab Last Tested Jun 25, 2025

Zanim zaczniesz

  1. Moduły tworzą projekt Google Cloud i zasoby na określony czas.
  2. Moduły mają ograniczenie czasowe i nie mają funkcji wstrzymywania. Jeśli zakończysz moduł, musisz go zacząć od początku.
  3. Aby rozpocząć, w lewym górnym rogu ekranu kliknij Rozpocznij moduł.

Użyj przeglądania prywatnego

  1. Skopiuj podaną nazwę użytkownika i hasło do modułu.
  2. Kliknij Otwórz konsolę w trybie prywatnym.

Zaloguj się w konsoli

  1. Zaloguj się z użyciem danych logowania do modułu. Użycie innych danych logowania może spowodować błędy lub naliczanie opłat.
  2. Zaakceptuj warunki i pomiń stronę zasobów przywracania.
  3. Nie klikaj Zakończ moduł, chyba że właśnie został przez Ciebie zakończony lub chcesz go uruchomić ponownie, ponieważ spowoduje to usunięcie wyników i projektu.

Ta treść jest obecnie niedostępna

Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem

Świetnie

Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem

Jeden moduł, a potem drugi

Potwierdź, aby zakończyć wszystkie istniejące moduły i rozpocząć ten

Aby uruchomić moduł, użyj przeglądania prywatnego

Uruchom ten moduł w oknie incognito lub przeglądania prywatnego. Dzięki temu unikniesz konfliktu między swoim kontem osobistym a kontem do nauki, co mogłoby spowodować naliczanie dodatkowych opłat na koncie osobistym.