title | description | ms.service | ms.custom | ms.date | monikerRange | recommendations |
---|---|---|---|---|---|---|
Use packages from PyPI |
How to consume packages from Python package index with Azure Artifacts |
azure-devops-artifacts |
devx-track-python |
11/17/2023 |
>= azure-devops-2019 |
true |
[!INCLUDE version-gt-eq-azure-devops-2019]
Using Azure Artifacts, developers can publish and consume packages from Azure Artifacts feeds and external registries such as pypi.org. This article will guide you through setting up your project and using the command line to efficiently consume Python packages from PyPI.
In this article, you'll learn how to:
[!div class="checklist"]
- Enable upstream sources for your feed
- Add PyPI as an upstream source
- Setup your project
- Install packages from Python package index
-
An Azure DevOps organization and a project. Create an organization or a project if you haven't already.
-
An Azure Artifacts feed.
-
Download Python.
If you haven't created a feed yet, follow the steps below to create a new one. Make sure to check the box for upstream sources to enable upstream sources. If you already have a feed, skip to the next step to add PyPI as an upstream source.
-
Sign in to your Azure DevOps organization, and then navigate to your project.
-
Select Artifacts, and then select Create Feed to create a new feed.
-
Enter a descriptive Name for your feed and define its Visibility (indicating who can view packages within the feed). Specify the Scope of your feed, and then check the Upstream sources checkbox to include packages from public registries.
-
Select Create when you're done.
:::image type="content" source="../media/new-feed-dialog-azure-devops.png" alt-text="A screenshot showing how to create a need feed.":::
If you selected the upstream sources checkbox during the creation of your feed, PyPI should have been automatically included as an upstream source. If not, you can manually add it by following these steps:
-
Sign in to your Azure DevOps organization, and then navigate to your project.
-
Select Artifacts, and then select the gear icon
to navigate to Feed Settings.
-
Select Upstream sources, and then select Add Upstream to add a new upstream source.
-
Select Public source, and then select PyPI (https://pypi.org/) from the dropdown menu.
-
Select Add when you're done, and then select Save one more time from the top right corner to save your changes.
-
Make sure you have downloaded Python, and then run the following command to upgrade your Python package manager:
python -m pip install --upgrade pip
-
Run the following command to install the Azure Artifacts keyring:
pip install keyring artifacts-keyring
-
Create a Personal access token with Packaging > Read scope to authenticate with Azure DevOps. The first time you connect to Azure DevOps, you'll need to enter your credentials when prompted. Provide your username (any string) and your personal access token in the designated fields. These credentials will be cached locally and automatically used to sign you in the next time you use the service.
-
Navigate to your project folder, and then run the following command to create a new virtual environment:
python -m venv <VIRTUAL_ENVIRONMENT_NAME>
-
Create a new pip.ini file (Windows) or a pip.conf file (Mac/Linux) in your virtual environment, and then paste the following snippet into your file. Make sure you replace the placeholders with the appropriate information, and be cautious not to commit this file to a public repository as it contains your personal access token.
-
Project-scoped feed:
[global] extra-index-url=https://<FEED_NAME>:<YOUR_PERSONAL_ACCESS_TOKEN>@pkgs.dev.azure.com/<ORGANIZATION_NAME>/<PROJECT_NAME>/_packaging/<FEED_NAME>/pypi/simple/
-
Organization-scoped feed:
[global] extra-index-url=https://<FEED_NAME>:<YOUR_PERSONAL_ACCESS_TOKEN>@pkgs.dev.azure.com/<ORGANIZATION_NAME>/_packaging/<FEED_NAME>/pypi/simple/
-
Now that we've configured our project to authenticate with our feed, we can begin installing packages from the PyPI upstream. In this example, we'll install Flask
:
-
In a command prompt window, navigate to your project folder and run the following command to activate your virtual environment. Replace the placeholder with the name of the virtual environment you created earlier:
<YOUR_VIRTUAL_ENVIRONMENT_NAME>/Scripts/Activate.ps1
-
Run the following command to check the packages installed in your virtual environment:
pip list
-
Run the following command to install Flask.
pip install -U Flask
-
Once your package is installed, Azure Artifacts will save a copy of this package to your feed. Your package should be available in your feed as shown in the screenshot below.
:::image type="content" source="media/install-package-from-upstream.png" alt-text="A screenshot showing packages installed from PyPI upstream.":::