Uredi

Deli z drugimi prek


Quickstart: Publish and consume Python packages with Azure Artifacts using the command line (CLI)

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

In this quickstart, you learn how to create an Azure Artifacts feed and use your feed to publish and consume Python packages from the command line in your local development environment. When you're finished, you have a Python package published to your feed and installed from your feed to your local development environment.

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.8 or later installed on your local machine. Download Python here.
  • pip 19.2 and twine 1.13.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.8 or later installed in your local development environment.
  • pip 19.2 and twine 1.13.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 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 with the following command using the URL you copied from your forked repository:

    git clone <REPOSITORY_URL>
    
  6. Change directory to the 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

Create a feed

  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.

Publish your package to your feed

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` 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 argument.

    twine upload --repository <FEED_NAME> dist/*
    

Install a package from your 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 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]
    extra-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>
    

Clean up resources

When you're finished with the resources you created, you can delete them to avoid incurring charges. When you delete a project, all its project level artifacts feeds are deleted.

To delete a project:

  1. Select Project Settings.
  2. On the Project details page, select Delete at the bottom of the page.
  3. Enter the name of the project to confirm, and then select Delete.

If you want to only delete the feed:

  1. Select Artifacts and select your feed from the drop-down menu.
  2. Select the settings button.
  3. From the Feed settings tab, select Delete feed.
  4. Select Delete to confirm.

To clean up your local development environment:

  1. To deactivate your virtual environment, run the following command:

    deactivate
    
  2. To delete your virtual environment, delete the directory where it was created.

  3. Remove the .pypirc file from your home directory.

If you cloned the sample Python package, you can delete the repository from your local machine and your GitHub account.

Next steps