首页 > 解决方案 > 如何在通过 MS DevOps 运行管道时在自托管(Linux Ubuntu 18.04)代理上指定 python 版本

问题描述

我正在尝试通过 MS DevOps 使用 yaml 管道运行一个简单的 bash 脚本。我想使用 Python 3.8 版。通过 SSH 连接到自托管代理时,我能够运行 yaml 文件中指定的脚本而没有任何问题。但是,当我通过管道运行脚本时,它会失败,因为由于某种原因python别名指的是 Python 2.7.17。

.bashrc我尝试在agentsvc用户和文件中设置以下别名bash.bashrcalias python='python3.8'.

我意识到我可以在我的脚本中更改pythonpython3.8,但是我不想这样做。有没有人知道为什么别名在管道环境中不起作用以及我能做些什么来解决这个问题?

这是我尝试运行的 yaml 管道示例:

pool: myubuntuagent

trigger:
  - master

steps:
  - script: |
      # install pre-requisites
      whoami
      python -V
      python -m pip install -r requirements.txt
    workingDirectory: '$(System.DefaultWorkingDirectory)'
    displayName: 'Install Prerequisites etc. etc. etc.'

标签: pythonazure-pipelinesazure-devops-self-hosted-agentazure-pipelines-yaml

解决方案


作为一种解决方法,我们可以在 cmd 下面尝试使用 Python3.x 版本

python3 -V
python3 -m pip install -r requirements.txt

结果:

在此处输入图像描述

此外,我们可以使用任务Use Python version来指定 python 版本。您可以参考此文档来配置您的自托管代理以使用此任务。

结果:

在此处输入图像描述


推荐阅读