How to install private python package from Azure Artifact feed via CLI

2 min read 06-10-2024
How to install private python package from Azure Artifact feed via CLI


Installing Private Python Packages from Azure Artifacts via CLI

Problem: You've developed a private Python package and stored it in Azure Artifacts. Now, you need to install it on your local machine using the command line.

Solution: This article guides you through the process of installing your private Python package from an Azure Artifacts feed via the command line.

Scenario: Imagine you have a Python package called 'my_private_package' hosted in your Azure Artifacts feed named 'my-feed'. You want to install this package on your machine using the pip command.

Original Code:

pip install my_private_package

Explanation:

This command will not work as 'my_private_package' is not publicly available on the Python Package Index (PyPI). To install a package from a private repository, you need to provide pip with the necessary information to access it.

Solution Breakdown:

  1. Authenticate with Azure Artifacts:

    • First, you need to authenticate with Azure Artifacts. This can be done through several methods:
      • Using the Azure CLI:
        az login
        az artifacts login --scope "my-feed" --resource-type "package" --source "my-organization" 
        
      • Using a personal access token (PAT):
        1. Generate a PAT in your Azure DevOps organization with appropriate permissions.
        2. Store it securely as an environment variable:
        export AZURE_DEVOPS_EXT_PAT="your_pat"
        
      • Using a service principal:
        1. Create a service principal with the necessary permissions.
        2. Store the service principal's credentials as environment variables.
  2. Configure the pip command:

    • Now that you're authenticated, you can use the pip command with the --extra-index-url flag to point to your Azure Artifacts feed:
    pip install --extra-index-url https://pkgs.dev.azure.com/my-organization/my-feed/_packaging/my-feed/pypi/simple/ my_private_package
    
    • Breakdown of the command:
      • --extra-index-url: Specifies an additional index URL for pip to search for packages.
      • https://pkgs.dev.azure.com/my-organization/my-feed/_packaging/my-feed/pypi/simple/: The URL of your Azure Artifacts feed, replace my-organization, my-feed with your actual values.
      • my_private_package: The name of your package.

Example:

Let's assume your Azure DevOps organization is called 'MyOrganization', your feed is named 'MyFeed' and your package is called 'MyPackage'. The command to install the package would be:

pip install --extra-index-url https://pkgs.dev.azure.com/MyOrganization/MyFeed/_packaging/MyFeed/pypi/simple/ MyPackage

Important Notes:

  • Authentication: Ensure you have the necessary permissions to access the feed and package.
  • Package Version: By default, pip will install the latest version of the package. You can specify a specific version using the == operator, e.g., MyPackage==1.0.0.
  • Private Feeds: Using private feeds can significantly improve security and control over your code.

Benefits of Using Azure Artifacts:

  • Private Repository: Host your Python packages securely within your organization.
  • Version Control: Easily manage and track different versions of your packages.
  • Team Collaboration: Collaborate with team members on package development and sharing.
  • Package Management: Streamline the process of distributing and consuming packages.

Further Resources:

By following these steps, you can successfully install private Python packages from Azure Artifacts via the command line, simplifying your package management process and enhancing your development workflow.