Managing Python packages

This page describes adding, viewing, and deleting Python packages and package versions.

Before you begin

  1. Verify that you have enabled the Artifact Registry API and installed Cloud SDK. See Enabling and disabling service for instructions.

    gcloud command-line tool version 329.0.0 is required to run commands for PyPI repositories. You can check the version with the command:

    gcloud version
    
  2. If the target repository does not exist, create a new Python package repository.

  3. Verify that Python 3 is installed. For installation instructions, see the Google Cloud tutorial for setting up Python.

  4. (Optional) Configure defaults for gcloud commands.

  5. If you are using keyring authentication with gcloud credentials, log in to Cloud SDK with your user account or service account.

Adding packages

Required permissions: Artifact Registry Writer

When you build a Python project, distribution files are saved in a dist subdirectory in your project. You can then use Twine to upload packages to the repository. We recommend that you use unique names for your private packages that do not match package names on pypi.org, particularly if your pip configuration includes multiple package indexes.

  1. Install Twine.

    pip install twine
    
  2. Change to your Python project directory.

  3. Upload the distribution files to the repository from the project dist subdirectory. Run the following command:

    twine upload dist/*
    

    You can specify a particular repository with the --repository-url flag.

    twine upload --repository-url https://LOCATION-python.pkg.dev/PROJECT/REPOSITORY/ dist/*
    

Viewing packages and versions

Required permissions: Artifact Registry Reader

To view packages and package versions using the Google Cloud Console or gcloud:

Console

  1. Open the Repositories page in the Google Cloud Console.

    Open the Repositories page

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of the package.

gcloud

To list packages in a repository, run the following command:

gcloud artifacts packages list [--repository=REPOSITORY] [--location=LOCATION]

Where

  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.

To view versions of a package, run the following command:

gcloud artifacts versions list --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION]

Where

  • PACKAGE is the ID of the package or fully qualified identifier for the package.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.

Deleting packages and versions

Required permissions: Artifact Registry Repository Administrator

To delete a package:

Console

  1. Open the Repositories page in the Google Cloud Console.

    Open the Repositories page

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Select the package that you want to delete.

  4. Click DELETE.

  5. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts packages delete PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] [--async]

Where

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • --async Return immediately, without waiting for the operation in progress to complete.

To delete versions of a package:

Console

  1. Open the Repositories page in the Google Cloud Console.

    Open the Repositories page

  2. In the repository list, click the appropriate repository.

    The Packages page lists the packages in the repository.

  3. Click a package to view versions of that package.

  4. Select versions that you want to delete.

  5. Click DELETE.

  6. In the confirmation dialog box, click DELETE.

gcloud

Run the following command:

gcloud artifacts versions delete VERSION \
    --package=PACKAGE \
    [--repository=REPOSITORY] [--location=LOCATION] \
    [--async]

Where

  • PACKAGE is the name of the package in the repository.
  • REPOSITORY is the name of the repository. If you configured a default repository, you can omit this flag to use the default.
  • LOCATION is a regional or multi-regional location. Use this flag to view repositories in a specific location. If you configured a default location, you can omit this flag to use the default.
  • --async returns immediately, without waiting for the operation in progress to complete.

Downloading packages

Required permissions: Artifact Registry Reader

Use the pip command to install packages from the repository.

Run the following command:

pip install PACKAGE

If you do not specify a repository, your pip configuration in pip.conf determines how pip searches for the package.

  • If you configured the Artifact Registry repository with the index-url setting and no other package indexes are configured, pip only searches your Artifact Registry repository for the package.
  • If you configured the repository with the extra-index-url setting or if you configured multiple package indexes, pip searches pypi.org and any other configured package indexes and chooses the latest version of the package.

Alternatively, you can specify the repository to search by adding the --index-url flag to the command.

pip install --index-url https://LOCATION-python.pkg.dev/PROJECT/REPOSITORY/simple/ PACKAGE

What's next