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.

Disclaimer:


The sample script provided below are adapted from third-party open source sites.

PowerShell script to install Python

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

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.

The script to install Python on Windows devices can be validated by running the python command from the device

Alternatively, IT admins can check the installed Python version by executing the following scripts:

PowerShell:

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.

After successfully executing the script to install Python on Windows, it shows the installed version as output in Hexnode

Batch:

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.

If Python is not installed on Windows, an error message will be displayed when running the script

Notes:

  • It is recommended to manually validate the script execution on a system before executing the action in bulk.
  • Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.

  • Sample Script Repository