Compartilhar via


Customize Python for Azure Pipelines

You can use Azure Pipelines to build your Python apps without having to set up any infrastructure of your own. Tools that you commonly use to build, test, and run Python apps - like pip - get pre-installed on Microsoft-hosted agents in Azure Pipelines.

To create your first pipeline with Python, see the Python quickstart.

Use a specific Python version

To use a specific version of Python in your pipeline, add the Use Python Version task to azure-pipelines.yml. This snippet sets the pipeline to use Python 3.11:

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '3.11'

Use multiple Python versions

To run a pipeline with multiple Python versions, for example to test a package against those versions, define a job with a matrix of Python versions. Then set the UsePythonVersion task to reference the matrix variable.

jobs:
- job: 'Test'
  pool:
    vmImage: 'ubuntu-latest' # other options: 'macOS-latest', 'windows-latest'
  strategy:
    matrix:
      Python38:
        python.version: '3.8'
      Python39:
        python.version: '3.9'
      Python310:
        python.version: '3.10'

  steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '$(python.version)'

You can add tasks to run using each Python version in the matrix.

Run Python scripts

To run Python scripts in your repository, use a script element and specify a filename. For example:

- script: python src/example.py

You can also run inline Python scripts with the Python Script task:

- task: PythonScript@0
  inputs:
    scriptSource: 'inline'
    script: |
      print('Hello world 1')
      print('Hello world 2')

To parameterize script execution, use the PythonScript task with arguments values to pass arguments into the executing process. You can use sys.argv or the more sophisticated argparse library to parse the arguments.

- task: PythonScript@0
  inputs:
    scriptSource: inline
    script: |
      import sys
      print ('Executing script file is:', str(sys.argv[0]))
      print ('The arguments are:', str(sys.argv))
      import argparse
      parser = argparse.ArgumentParser()
      parser.add_argument("--world", help="Provide the name of the world to greet.")
      args = parser.parse_args()
      print ('Hello ', args.world)
    arguments: --world Venus

Install dependencies

You can use scripts to install specific PyPI packages with pip. For example, this YAML installs or upgrades pip and the setuptools and wheel packages.

- script: python -m pip install --upgrade pip setuptools wheel
  displayName: 'Install tools'

Install requirements

After you update pip and friends, a typical next step is to install dependencies from requirements.txt:

- script: pip install -r requirements.txt
  displayName: 'Install requirements'

Run tests

Use scripts to install and run various tests in your pipeline.

Run lint tests with flake8

To install or upgrade flake8 and use it to run lint tests, use this YAML:

- script: |
    python -m pip install flake8
    flake8 .
  displayName: 'Run lint tests'

Test with pytest and collect coverage metrics with pytest-cov

Use this YAML to install pytest and pytest-cov, run tests, output test results in JUnit format, and output code coverage results in Cobertura XML format:

- script: |
    pip install pytest pytest-azurepipelines
    pip install pytest-cov
    pytest --doctest-modules --junitxml=junit/test-results.xml --cov=. --cov-report=xml
  displayName: 'pytest'