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.
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 with the following command using the URL you copied from your forked repository:
git clone <REPOSITORY_URL>
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
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.
Publish your package to your feed
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
argument.twine upload --repository <FEED_NAME> dist/*
Install a package from your feed
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] extra-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>
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:
- Select Project Settings.
- On the Project details page, select Delete at the bottom of the page.
- Enter the name of the project to confirm, and then select Delete.
If you want to only delete the feed:
- Select Artifacts and select your feed from the drop-down menu.
- Select the settings button.
- From the Feed settings tab, select Delete feed.
- Select Delete to confirm.
To clean up your local development environment:
To deactivate your virtual environment, run the following command:
deactivate
To delete your virtual environment, delete the directory where it was created.
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.