Share via

Validating and Installing Python on Container Agents

tarun k 635 Reputation points
2025-06-25T04:34:22.61+00:00

Validating the Python installation on the container agent, installing Python if it is not present, and then validating the installation again to ensure it is correctly set up

Azure DevOps
0 comments No comments

Answer accepted by question author

Durga Reshma Malthi 11,600 Reputation points Microsoft External Staff Moderator
2025-06-25T08:31:21.75+00:00

Hi tarun k

As we discussed in private message,

To Check if Python is Installed in the Container, you can try the below commands in PowerShell inside the container:

  • python --version
  • pip --version
  • where.exe python or where python

Your Linux container is using yum install -y to set up Python and related packages in a layered, declarative way. To mirror that same simplicity and reliability in your Windows Server Core container, we’ll emulate it using a PowerShell-based install for Python.

# Install Python 3.11 and pip
RUN Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.11.4/python-3.11.4-amd64.exe" -OutFile "python-installer.exe" ; `
    Start-Process -Wait -FilePath "python-installer.exe" -ArgumentList '/quiet InstallAllUsers=1 PrependPath=1' ; `
    Remove-Item "python-installer.exe"
# Verify Python and pip
RUN python --version
RUN pip --version

Place this docker file before the WORKDIR /azp section.

Alternatively, you can try the below steps.

  1. In your Dockerfile, you already have this line: COPY install_build_tools/* ./
  2. I suggest you create install_python.ps1 "inside install_build_tools/"
  3. Add this script in install_python.ps1
       param (
           [string]$AUSER,
           [string]$APASS
       )
       Write-Host "Installing Python 3.12..."
       $pythonInstaller = "python-3.12.2-amd64.exe"
       Invoke-WebRequest -Uri "https://www.python.org/ftp/python/3.12.2/$pythonInstaller" -OutFile $pythonInstaller
       Start-Process -FilePath .\$pythonInstaller -ArgumentList "/quiet InstallAllUsers=1 PrependPath=1" -Wait
       Remove-Item -Force $pythonInstaller
       Write-Host "✅ Python installation complete"
       python --version
    
  4. Now add the below line in the docker file below the #Install Visual Studio Build Tools:
       # Install Python
       RUN powershell -ExecutionPolicy Bypass -File install_python.ps1 $AUSER $env:AUSER $APASS $env:APASS
    

Hope this helps!

Please Let me know if you have any queries.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Abiola Akinbade 30,490 Reputation points Volunteer Moderator
    2025-06-25T08:20:52.44+00:00

    Something like this should fulfill your ask:

    steps:
    - script: |
        echo "Checking for existing Python installation..."
        python --version || echo "Python is not installed."
        which python || echo "Python path not found."
      displayName: '🔍 Validate Existing Python Installation'
    
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '3.10'
        addToPath: true
      displayName: 'Install Python 3.10'
    
    - script: |
        echo "Re-validating Python installation..."
        python --version
        which python   
    	displayName: ' Confirm Python Installation'
    
    

    See: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/use-python-version-v0?view=azure-pipelines

    You can mark it 'Accept Answer' and 'Upvote' if this helped you

    Regards,

    Abiola

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.