Use Python Version task

Azure Pipelines

Use this task in a build or release pipeline to select a version of Python to run on an agent, and optionally add it to PATH.

Demands

None

Prerequisites

  • A Microsoft-hosted agent with side-by-side versions of Python installed, or a self-hosted agent with Agent.ToolsDirectory configured (see Q&A).

This task will fail if no Python versions are found in Agent.ToolsDirectory. Available Python versions on Microsoft-hosted agents can be found here.

Note

x86 and x64 versions of Python are available on Microsoft-hosted Windows agents, but not on Linux or macOS agents.

YAML snippet

# Use Python version
# Use the specified version of Python from the tool cache, optionally adding it to the PATH
- task: UsePythonVersion@0
  inputs:
    #versionSpec: '3.x' 
    #addToPath: true 
    #architecture: 'x64' # Options: x86, x64 (this argument applies only on Windows agents)

Arguments

Argument Description Default
Version spec Version range or exact version of a Python version to use. 3.x
Add to PATH Whether to prepend the retrieved Python version to the PATH environment variable to make it available in subsequent tasks or scripts without using the output variable. true
Advanced - Architecture The target architecture (x86, x64) of the Python interpreter. x86 is supported only on Windows. x64

As of version 0.150 of the task, version spec will also accept pypy2 or pypy3.

If the task completes successfully, the task's output variable will contain the directory of the Python installation:

output variable

Remarks

After running this task with "Add to PATH," the python command in subsequent scripts will be for the highest available version of the interpreter matching the version spec and architecture.

The versions of Python installed on the Microsoft-hosted Ubuntu and macOS images follow the symlinking structure for Unix-like systems defined in PEP 394. For example, for Python 3.7, python3.7 is the actual interpreter. python3 is symlinked to that interpreter, and python is a symlink to that symlink.

On the Microsoft-hosted Windows images, the interpreter is just python.

For Microsoft-hosted agents, x86 is supported only on Windows. This is because Windows can run executables compiled for the x86 architecture with the WoW64 subsystem. Hosted Ubuntu and Hosted macOS run 64-bit operating systems and run only 64-bit Python.

Open source

This task is open source on GitHub. Feedback and contributions are welcome.

Q & A

Where can I learn more about tool installers?

For an explanation of tool installers and examples, see Tool installers.

Do I need an agent?

You need at least one agent to run your build or release. Get an agent for Linux, macOS, or Windows.

I'm having problems. How can I troubleshoot them?

See Troubleshoot Build and Release.

I can't select a default agent pool and I can't queue my build or release. How do I fix this?

See Agent pools.

How can I configure a self-hosted agent to use this task?

Our image generation scripts are open-source on GitHub. Search the code for AGENT_TOOLSDIRECTORY.

For details on how the task finds a particular Python version, see here. The tool name to use is "Python." In order that your scripts may work as they would on Microsoft-hosted agents, we recommend following the symlinking structure from PEP 394 on Unix-like systems.