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.
Sign in to your Azure DevOps organization, and then go to your project.
Select Artifacts, and then select Create Feed.
Enter the following information for your feed:
- Enter a descriptive Name for your feed.
- Define its Visibility (indicating who can view packages within the feed).
- Select whether to use packages from public sources such as pypi.org.
- Specify the Scope of your feed.
Select Create.
Go to your Azure DevOps collection, select your project.
Select Artifacts, and then select Create Feed.
Enter the following information for your feed:
- Enter a descriptive Name for your feed.
- Define its Visibility (indicating who can view packages within the feed).
- Select whether to use packages from public sources such as pypi.org.
- Specify the Scope of your feed.
Select Create.
Go to your Azure DevOps collection, select your project.
Select Artifacts, and then select Create Feed to create a new feed.
Enter the following information for your feed:
- Enter a descriptive Name for your feed.
- Define its Visibility (indicating who can view packages within the feed).
- Select whether to use packages from public sources such as pypi.org.
- Specify the Scope of your feed.
Select Create.
Go to your Azure DevOps collection, select your project.
Select Artifacts, and then select Create Feed to create a new feed.
Enter the following information for your feed:
- Enter a descriptive Name for your feed.
- Define its Visibility (indicating who can view packages within the feed).
- Choose whether to use packages from public sources such as pypi.org.
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.
Go to the following GitHub repository:
https://github.com/microsoft/python-package-template
Fork the repository to your GitHub account.
Go to your forked repository, and select Code.
Copy the URL of your forked repository.
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 <REPOSITORY_URL>
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:
- Use the artifacts-keyring package, which automatically sets up authentication for you.
- 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
Select Connect to feed from your feed.
Select twine and copy the repository URL from the Project setup section.
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.
Go to your Azure DevOps Project and select Artifacts.
Select your feed and select Connect to feed.
Select twine under the Python section.
On your development machine, ensure that twine is installed.
pip install --upgrade twine
Follow the instructions in the Project setup section to set up your
.pypirc
file.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>
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
In your project, select Artifacts and then select your feed.
Select Connect to feed.
Select pip under the Python section.
Prepare your local Python environment.
Ensure pip is installed and up to date:
python -m pip install --upgrade pip
To create and activate a Python virtual environment:
python -m venv myenv myenv/Scripts/activate
Copy the
index-url
from the Project setup section of the Connect to feed dialog.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
Go to your Azure DevOps Project and select Artifacts.
Select your feed and select Connect to feed.
Select pip under the Python section.
Prepare your local Python environment.
Ensure pip is installed and up to date:
python -m pip install --upgrade pip
Create and activate a Python virtual environment:
python -m venv myenv myenv/Scripts/activate
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.
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>
To install your package, run the following command replacing <PACKAGE_NAME> with the package name from your feed.
pip install <PACKAGE_NAME>