Редагувати

Поділитися через


Publish and consume Python packages using the command line (CLI)

Azure DevOps Services | Azure DevOps Server 2022 - Azure DevOps Server 2019

Azure Artifacts allows you to create, host, and share Python packages with your team or organization. You can publish and consume Python packages using an Azure Artifacts feed from the command line in your local development environment.

In this article you learn how to:

  • Create an Azure Artifacts feed.
  • Set up authentication with either the Python artifacts-keyring package or, a personal access token (PAT).
  • Publish Python packages to your feed.
  • Consume Python packages from your feed.

To publish and consume packages in your Azure Pipelines, see Publish Python packages with Azure Pipelines.

Prerequisites

To run the following steps, you must have:

  • An Azure DevOps organization. Create one for free.
  • A personal access token (PAT) with Packaging > Read scope. To create one, see Create a PAT.
  • An Azure DevOps project. If you don't have one, create a project.
  • Python 3.9 or later installed on your local machine. Download Python here.
  • pip 24.0 and twine 5.0.0 or higher.
  • A Python package to publish from your local machine to your feed.
  • If using the sample Python package:
    • A GitHub account. Create a free GitHub account if you don't have one already.
    • git installed on your local machine.
  • A GitHub account. Create a free GitHub account if you don't have one already.
  • Access to an Azure DevOps Server collection.
  • A personal access token (PAT) with Packaging > Read scope. To create one, see Create a PAT.
  • An Azure DevOps project. If you don't have one, create a project.
  • Python 3.9 or later installed in your local development environment.
  • pip 24.0 and twine 5.0.0 or higher.
  • If using the sample Python package:
    • A GitHub account. Create a free GitHub account if you don't have one already.
    • git installed on your local machine.

Create an Azure Artifacts feed

If you don't have a feed, use the following steps to create one.

  1. Sign in to your Azure DevOps organization, and then go to your project.

  2. Select Artifacts, and then select Create Feed.

    A screenshot showing how to create a  feed.

  3. Enter the following information for your feed:

    1. Enter a descriptive Name for your feed.
    2. Define its Visibility (indicating who can view packages within the feed).
    3. Select whether to use packages from public sources such as pypi.org.
    4. Specify the Scope of your feed.
  4. Select Create.

  1. Go to your Azure DevOps collection, select your project.

  2. Select Artifacts, and then select Create Feed.

    A screenshot showing how to create a  feed in DevOps Server.

  3. Enter the following information for your feed:

    1. Enter a descriptive Name for your feed.
    2. Define its Visibility (indicating who can view packages within the feed).
    3. Select whether to use packages from public sources such as pypi.org.
    4. Specify the Scope of your feed.
  4. Select Create.

  1. Go to your Azure DevOps collection, select your project.

  2. Select Artifacts, and then select Create Feed to create a new feed.

  3. Enter the following information for your feed:

    1. Enter a descriptive Name for your feed.
    2. Define its Visibility (indicating who can view packages within the feed).
    3. Select whether to use packages from public sources such as pypi.org.
    4. Specify the Scope of your feed.
  4. Select Create.

  1. Go to your Azure DevOps collection, select your project.

  2. Select Artifacts, and then select Create Feed to create a new feed.

  3. Enter the following information for your feed:

    1. Enter a descriptive Name for your feed.
    2. Define its Visibility (indicating who can view packages within the feed).
    3. Choose whether to use packages from public sources such as pypi.org.
  4. Select Create.

Create a local Python package

You need a Python package to publish to your feed. If you don't have a package to publish, you can clone a sample Python package from GitHub.

Clone the sample Python package

Use the following steps to use the sample Python package from GitHub.

  1. Go to the following GitHub repository:

    https://github.com/microsoft/python-package-template
    
  2. Fork the repository to your GitHub account.

  3. Go to your forked repository, and select Code.

  4. Copy the URL of your forked repository.

  5. From a CLI on your local machine, clone the repository to your local machine using the URL you copied from your forked repository.

    git clone <REPOSIORY_URL>
    
  6. Change directory to your cloned repository.

    cd python-package-template
    

Build your package

To build your wheel and source distribution, run the following commands in your project directory:

pip install --upgrade build
python -m build

If your Python project has a setup.py file, you can use the following command to build your package:

python setup.py sdist bdist_wheel

Connect to feed

There are two primary ways to connect to a feed to publish or consume your Python packages:

  1. Use the artifacts-keyring package, which automatically sets up authentication for you.
  2. Manually set up credentials with a PAT.

Note

artifacts-keyring is not supported on newer versions of Ubuntu.

Note

If your organization is using a firewall or a proxy server, make sure you allow Azure Artifacts Domain URLs and IP addresses.

Set up artifacts-keyring for authentication

The artifacts-keyring package works with the Python keyring package to allow you to set up authentication to publish and consume your Python packages to and from your feed. Both pip and twine use the Python keyring package to find credentials.

Important

You must have pip 19.2 and twine 1.13.0 or higher to use artifacts-keyring. For more information, see Usage requirements.

If you choose to use artifacts-keyring, you must install the package before you can use it.

In an elevated command prompt window, run the following command to install the artifacts-keyring package:

pip install artifacts-keyring

Publish Python packages

You can publish Python packages to your feed using the artifacts-keyring package or PAT authentication.

Publish packages with artifacts-keyring

  1. Select Connect to feed from your feed.

    A screenshot highlighting the connect to feed.

  2. Select twine and copy the repository URL from the Project setup section.

    A screenshot of instructions to connect to feed with twine.

  3. To publish a package to your feed, run the following command replacing <FEED_URL> with the repository URL you copied from the Connect to feed dialog:

    twine upload --repository-url <FEED_URL> dist/*
    

Publish packages with PAT authentication

Use twine to upload your package to your Azure Artifacts feed.

  1. Go to your Azure DevOps Project and select Artifacts.

  2. Select your feed and select Connect to feed.

    A screenshot highlighting the connect to feed.

  3. Select twine under the Python section.

    A screenshot highlighting the twine package type.

  4. On your development machine, ensure that twine is installed.

    pip install --upgrade twine
    
  5. Follow the instructions in the Project setup section to set up your .pypirc file.

    A screenshot highlighting the `.pyirc` file content.

  6. To avoid needing to enter your personal access token every time you publish a package, you can add your credentials to the .pypirc file. Ensure that you don't check your personal access token into a public repository.

    Example of a .pypirc file with credentials:

    [distutils]
    Index-servers =
        <FEED_NAME>
    
    [<FEED_NAME>]
    Repository = <FEED_URL>
    username = <FEED_NAME>
    password = <YOUR_PERSONAL_ACCESS_TOKEN>
    
  7. To upload your package, run the following command in your project directory replacing <FEED_NAME> with your feed name. On Windows, you might need to specify the pypirc file location with the --config-file option.

    twine upload --repository <FEED_NAME> dist/*
    

Consume Python packages

You can consume Python packages from your feed using the artifacts-keyring package or PAT authentication.

Consume packages with artifacts-keyring

  1. In your project, select Artifacts and then select your feed.

  2. Select Connect to feed.

    A screenshot highlighting the connect to feed button.

  3. Select pip under the Python section.

    A screenshot of pip selection in Connect to feed.

  4. Prepare your local Python environment.

    1. Ensure pip is installed and up to date:

      python -m pip install --upgrade pip
      
    2. To create and activate a Python virtual environment:

      python -m venv myenv
      myenv/Scripts/activate
      
  5. Copy the index-url from the Project setup section of the Connect to feed dialog.

    A screenshot of the index-url in the Connect to feed dialog.

  6. To install a package from your feed, run the following command replacing <PACKAGE_NAME> with the package name from your feed and <INDEX_URL> with the index url you copied from the Connect to feed dialog:

    pip install <PACKAGE_NAME> --index-url <INDEX_URL>
    

When you connect to Azure DevOps for the first time, you're prompted for credentials. Enter your user name (any string) and your personal access token in the appropriate fields. The credentials will be cached locally and used to automatically sign you in the next time you use the service.

Consume packages with PAT authentication

  1. Go to your Azure DevOps Project and select Artifacts.

  2. Select your feed and select Connect to feed.

    A screenshot highlighting the connect to feed button.

  3. Select pip under the Python section.

    A screenshot highlighting the pip package type.

  4. Prepare your local Python environment.

    1. Ensure pip is installed and up to date:

      python -m pip install --upgrade pip
      
    2. Create and activate a Python virtual environment:

      python -m venv myenv
      myenv/Scripts/activate
      
  5. Add a pip.ini (Windows) or a pip.conf (Mac/Linux) file to the root directory of your virtual environment. Copy the content from the Project setup section of the Connect to feed dialog and add it to your pip.ini or pip.conf file.

    A screenshot highlighting the pip.ini file content.

  6. To avoid needing to enter your personal access token every time you install a package from your feed, you can add your credentials to the pip.ini or pip.conf file. Make sure you don't check your personal access token into a public repository.

    Example of a pip.ini or pip.conf file with credentials:

    [global]
    index-url=https://<FEED_NAME>:<YOUR_PERSONAL_ACCESS_TOKEN>@<FEED_URL>
    
  7. To install your package, run the following command replacing <PACKAGE_NAME> with the package name from your feed.

    pip install <PACKAGE_NAME>