I have a similar problem but on a Linux machine. I'd summarize the problem and solution, which is already raised in the https://github.com/microsoft/azure-pipelines-tasks/issues/19255 GitHub issue.
Basically, the UsePythonVersion@0
expected to work on hosted agent out of the box. In self-hosted agent, you'll need some tinkering. Add the downloaded Python library folder to the LD_LIBRARY_PATH path:
~/.bashrc
file and set the library path:export LD_LIBRARY_PATH=/home/<your_agent_name>/agent/_work/_tool/Python/3.12.8/x64/lib
On Ubuntu machine, the libpython3.12.so.1.0
library file can be found at the /home/<your_agent_name>/agent/_work/_tool/Python/3.12.8/x64/lib
path.
variables:
pythonVersion: '3.12.8'
jobs:
- job:
pool: <self_hosted_agent>
steps:
- script: |
echo "##vso[task.setvariable variable=LD_LIBRARY_PATH;]:/home/<your_agent_name>/agent/_work/_tool/Python/$(pythonVersion)/x64/lib"
displayName: Add the Python library path to the Linux native code library path
- task: UsePythonVersion@0
inputs:
versionSpec: $(pythonVersion)
displayName: Use Python $(pythonVersion) version
Importantly, you need to specify the exact Python version in order to set the proper path.