Category filter
Script to install Python on Windows devices
Python is a powerful and versatile programming language with applications ranging from web development and data analysis to scientific computing and beyond.
In IT environments where Python is widely used, it’s crucial to ensure that all developers have the required version of Python installed for their specific projects. This not only prevents compatibility issues during collaborative projects but also guarantees that developers have access to the necessary features, bug fixes, security updates, and associated libraries or packages specific to that version. For example, a team or department may be working on a project that requires a specific Python version and associated libraries or packages. In such cases, using a different version of Python could lead to compatibility issues, potentially delaying the project and introducing errors. To ensure consistency across developer environments, IT administrators can utilize Hexnode’s Execute Custom Script action to seamlessly install the required version of Python on Windows devices by executing the provided scripts.
PowerShell script to install Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Define the Python version and download URL $pythonVersion = "<version>" $pythonDownloadUrl = "https://www.python.org/ftp/python/$pythonVersion/python-$pythonVersion-amd64.exe" # Define the installation directory $installDir = "C:\Python" # Download Python installer Invoke-WebRequest -Uri $pythonDownloadUrl -OutFile "$env:TEMP\python-installer.exe" # Install Python silently Start-Process -FilePath "$env:TEMP\python-installer.exe" -ArgumentList "/quiet", "InstallAllUsers=1", "PrependPath=1", "DefaultCustomInstall=1", "DefaultPath=$installDir" -Wait # Clean up Remove-Item "$env:TEMP\python-installer.exe" -Force |
This PowerShell script begins by defining variables for the desired Python version and its download URL. The placeholder “version” can be replaced with the specific version number needed. Subsequently, the script defines the installation directory path. Utilizing the Invoke-WebRequest cmdlet, the script downloads the Python installer from the specified URL and saves it in a temporary directory. To ensure a silent installation without any user interface or interaction, the script employs the Start-Process cmdlet with the /quiet argument. The script waits for the installation to finish using the -Wait parameter. It then cleans up by removing the downloaded Python installer from the temporary directory, bypassing confirmation prompts with the -Force parameter.
Batch script to install Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@echo off rem Define the Python version and download URL set "pythonVersion=<version>" set "pythonDownloadUrl=https://www.python.org/ftp/python/%pythonVersion%/python-%pythonVersion%-amd64.exe" rem Define the installation directory set "installDir=C:\Python" rem Download Python installer bitsadmin.exe /transfer "PythonInstaller" "%pythonDownloadUrl%" "%TEMP%\python-installer.exe" rem Install Python silently "%TEMP%\python-installer.exe" /quiet InstallAllUsers=1 PrependPath=1 DefaultCustomInstall=1 DefaultPath=%installDir% /wait rem Clean up del "%TEMP%\python-installer.exe" /f /q |
This batch script starts by defining variables for the desired Python version and its download URL. The placeholder “version” can be substituted with the specific version number needed. Next, the installation directory is defined. The script then employs the bitsadmin.exe command-line tool to download the Python installer from the specified URL to a temporary location. After the download is complete, the script executes the Python installer to install Python silently using the /quiet argument. Finally, the script deletes the Python installer executable from the temporary location to clean up.
What happens at the device end?
After executing the script, Python will be silently installed on the Windows device. At the device end, IT admins can use the “python” command in the PowerShell terminal to verify the installation of Python. This command checks if Python is installed and displays the installed Python version.
Alternatively, IT admins can check the installed Python version by executing the following scripts:
PowerShell:
1 2 3 4 5 6 7 8 9 10 11 12 |
# Check if python is installed $pythonInstalled = Get-Command python -ErrorAction SilentlyContinue if ($pythonInstalled) { # Get the Python version $pythonVersion = python --version 2>&1 # Print the Python version Write-Host "Python version: $pythonVersion" } else { Write-Host "Python is not installed on this device." } |
This PowerShell script checks for the presence of Python on a system by attempting to find the python command in the system’s PATH. If Python is installed, it retrieves its version using python –version. The script then prints the Python version to the console. If Python is not found, it displays a message indicating its absence.
Batch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
@echo off setlocal set "python_exe=python" REM Check if Python executable exists where %python_exe% >nul 2>nul if %errorlevel% neq 0 ( echo Python is not installed on this device. goto :eof ) REM Get Python version for /f "tokens=*" %%v in ('%python_exe% -V 2^>^&1') do set "python_version=%%v" echo Python Version: %python_version% endlocal |
This batch script first defines the variable python_exe as “python”, representing the Python executable name. It then checks if Python is installed by attempting to locate its executable using where command. If the executable is not found (i.e., the error level is not equal to 0), the script outputs a message indicating that Python is not installed. If Python is found, it retrieves its version using the %python_exe% -V command and displays it.