user8701560
user8701560

Reputation: 51

Azure Self hosted agent to run pytest

I have installed a self-hosted agent on my local VM, it's connected to azure no issues there. I have a python code on azure DevOps I have installed all the requirements.txt requirements manually into the cmd line of the local VM so that self-hosted agent installed on it doesn't have to install them ( to minimize the build & deployment time)

But when I have below code in the YAML file to run pytest cases pipeline is failing because of below error

This is my Yaml file

    trigger:
        - master
        variables:
          python.version : 3.8.6

        stages:
        - stage: Build
          jobs: 
          - job: Build
            pool:
              name: 'MaitQA'
            #pool:
            #  vmImage: 'windows-latest'   # windows-latest Or windows-2019 ; vs2017-win2016 # https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml#software # vs2017-win2016

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

        - script: 'pip install pytest pytest-azurepipelines ; pytest unit_test/'
                

This is The error --------------- Starting: Use Python 3.8.6 ------------------------------ Task : Use Python version Description : Use the specified version of Python from the tool cache, optionally adding it to the PATH Version : 0.151.4 Author : Microsoft Corporation Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/tool/use-python-version ------------------------------------------- [error]Version spec 3.8.6 for architecture x64 did not match any version in Agent.ToolsDirectory. Versions in C:\CodeVersions_tool: If this is a Microsoft-hosted agent, check that this image supports side-by-side versions of Python at https://aka.ms/hosted-agent-software. If this is a self-hosted agent, see how to configure side-by-side Python versions at https://go.microsoft.com/fwlink/?linkid=871498. Finishing: Use Python 3.8.6 ---------------

Upvotes: 3

Views: 4703

Answers (3)

JStrahl
JStrahl

Reputation: 484

This error refers to Python not being in the agent tools directory, and therefore unavailable to the agent.

Here are (incomplete) details for setting up the tools directory with Python:

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/use-python-version?view=azure-devops#how-can-i-configure-a-self-hosted-agent-to-use-this-task

The mystery in the above documentation is, what are those 'tool_files' they refer to?

Thankfully, jrm346 on GitHub went through the source code to work it out; for Linux you need to compile Python from source and reconfigure the target directory:

https://github.com/microsoft/azure-pipelines-tasks/issues/10721

For Python 3.8:

  1. Create the needed file structure under the agent's tool's directory:

    Python

     └── 3.8.0
         ├── x64
         └── x64.complete
    

Then compile Python 3.8.6 following the below instructions, with one small addition: just after '/configure --enable-optimizations' of step 4 run the command './configure --prefix=/home/azure/_work/_tool/Python/3.8.0/x64', replacing '/home/azure/_work/_tool' with your agent's tools directory location:

https://linuxize.com/post/how-to-install-python-3-8-on-ubuntu-18-04/

Upvotes: 2

Cece Dong - MSFT
Cece Dong - MSFT

Reputation: 31075

Including @Krzysztof Madej's suggestion, you can also try to restart the self-hosted agent service.

Upvotes: 0

Krzysztof Madej
Krzysztof Madej

Reputation: 40909

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

The desired Python version will have to be added to the tool cache on the self-hosted agent in order for the task to use it. Normally the tool cache is located under the _work/_tool directory of the agent or the path can be overridden by the environment variable AGENT_TOOLSDIRECTORY. Under that directory, create the following directory structure based off of your Python version:

Upvotes: 0

Related Questions