title | description | ms.topic | ms.assetid | ms.date | monikerRange | ms.custom |
---|---|---|---|---|---|---|
Build and test Python apps |
Automatically build and test Python apps with Azure Pipelines |
quickstart |
141149f8-d1a9-49fa-be98-ee9a825a951a |
02/23/2022 |
>=azure-devops-2019 |
devx-track-python, freshness-fy22q2 |
[!INCLUDE version-gt-eq-2019]
You can use Azure Pipelines to build, test, and deploy Python apps and scripts as part of your CI/CD system. This article focuses on creating a basic pipeline.
If you want an end-to-end tutorial, see Use CI/CD to deploy a Python web app to Azure App Service on Linux.
To create and activate an Anaconda environment and install Anaconda packages with conda
, see Run pipelines with Anaconda environments.
::: moniker range=">=azure-devops-2020"
New to Azure Pipelines? If so, then we recommend you try this section before moving on to other sections.
::: moniker-end
::: moniker range="azure-devops-2019"
Import this repo into your Git repo in Azure DevOps Server 2019:
::: moniker-end
::: moniker range=">=azure-devops-2020"
Import this repo into your Git repo:
::: moniker-end
https://github.com/Microsoft/python-sample-vscode-flask-tutorial
::: moniker range=">=azure-devops-2020"
[!INCLUDE include]
[!INCLUDE include]
::: moniker-end
::: moniker range=">=azure-devops-2020"
[!INCLUDE include]
When the Configure tab appears, select Python package to create a Python package to test on multiple Python versions.
-
When your new pipeline appears, take a look at the YAML to see what it does. When you're ready, select Save and run.
-
You're prompted to commit a new azure-pipelines.yml file to your repository. After you're happy with the message, select Save and run again.
If you want to watch your pipeline in action, select the build job.
You just ran a pipeline that we automatically created for you, because your code appeared to be a good match for the Python package template.
You now have a working YAML pipeline (
azure-pipelines.yml
) in your repository that's ready for you to customize! -
When you're ready to make changes to your pipeline, select it in the Pipelines page, and then Edit the
azure-pipelines.yml
file.
Read further to learn some of the more common ways to customize your pipeline.
::: moniker-end
::: moniker range="azure-devops-2019"
- Add an
azure-pipelines.yml
file in your repository. Customize this snippet for your build.
trigger:
- master
pool: Default
steps:
- script: python -m pip install --upgrade pip
displayName: 'Install dependencies'
- script: pip install -r requirements.txt
displayName: 'Install requirements'
-
Create a pipeline (if you don't know how, see Create your first pipeline), and for the template select YAML.
-
Set the Agent pool and YAML file path for your pipeline.
-
Save the pipeline and queue a build. When the Build #nnnnnnnn.n has been queued message appears, select the number link to see your pipeline in action.
-
When you're ready to make changes to your pipeline, Edit it.
Read further to learn some of the more common ways to customize your pipeline.
::: moniker-end
::: moniker range=">=azure-devops-2020"
You don't have to set up anything for Azure Pipelines to build Python projects. Python is preinstalled on Microsoft-hosted build agents for Linux, macOS, or Windows. To see which Python versions are preinstalled, see Use a Microsoft-hosted agent.
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.6:
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '3.6'
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:
Python27:
python.version: '2.7'
Python35:
python.version: '3.5'
Python36:
python.version: '3.6'
steps:
- task: UsePythonVersion@0
inputs:
versionSpec: '$(python.version)'
You can add tasks to run using each Python version in the matrix.
::: moniker-end
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
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'
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'
Use scripts to install and run various tests in your pipeline.
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'
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'
::: moniker range=">=azure-devops-2020"
Azure Pipelines can run parallel Tox test jobs to split up the work. On a development computer, you have to run your test environments in series. This sample uses tox -e py
to run whichever version of Python is active for the current job.
- job:
pool:
vmImage: 'ubuntu-latest'
strategy:
matrix:
Python27:
python.version: '2.7'
Python35:
python.version: '3.5'
Python36:
python.version: '3.6'
Python37:
python.version: '3.7'
steps:
- task: UsePythonVersion@0
displayName: 'Use Python $(python.version)'
inputs:
versionSpec: '$(python.version)'
- script: pip install tox
displayName: 'Install Tox'
- script: tox -e py
displayName: 'Run Tox'
Add the Publish Test Results task to publish JUnit or xUnit test results to the server:
- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
testResultsFiles: '**/test-*.xml'
testRunTitle: 'Publish test results for Python $(python.version)'
Add the Publish Code Coverage Results task to publish code coverage results to the server. You can see coverage metrics in the build summary and download HTML reports for further analysis.
- task: PublishCodeCoverageResults@1
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
To authenticate with twine
, use the Twine Authenticate task to store authentication credentials in the PYPIRC_PATH
environment variable.
- task: TwineAuthenticate@0
inputs:
artifactFeed: '<Azure Artifacts feed name>'
pythonUploadServiceConnection: '<twine service connection from external organization>'
Then, add a custom script that uses twine
to publish your packages.
- script: |
twine upload -r "<feed or service connection name>" --config-file $(PYPIRC_PATH) <package path/files>
You can also use Azure Pipelines to build an image for your Python app and push it to a container registry.
::: moniker-end
- Azure DevOps plugin for PyCharm (IntelliJ) (Microsoft)
- Python in Visual Studio Code (Microsoft)