How to Install Python With PowerShell

  1. Configuring the Execution Policy
  2. Install Chocolatey Using PowerShell
  3. Use Chocolately to Install Python
How to Install Python With PowerShell

Python is a versatile programming language that we can use for many different programming projects. For example, we can install Python manually using an executable file, or we can use PowerShell as well.

This article will discuss how to install Python manually through a package manager called Chocolatey.

Configuring the Execution Policy

Before proceeding, ensure that any external scripts we run are correctly signed. To do this, we will use the RemoteSigned execution policy to set the permission for the current user.

This approach prevents the permissions from becoming as broad as they would be with Unrestricted permission while enabling PowerShell to accept downloaded scripts that we trust.

In PowerShell, type the following code:

Set-ExecutionPolicy -Scope CurrentUser

When PowerShell asks us for an execution policy, we respond as follows:

RemoteSigned

Once we press the Enter key, click Yes or Yes to All and allow the changes to take effect. Next, confirm the changes by typing the following code.

Example Code:

Get-ExecutionPolicy -List

Output:

Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser    RemoteSigned
 LocalMachine       Undefined

This output verifies the user’s ability to execute reliable scripts downloaded from the internet. Now we have set up our Python programming environment; we can proceed to download the necessary files.

Install Chocolatey Using PowerShell

A package manager is a group of software tools that automates various aspects of software installation, such as initial setup, upgrades, configuration, and removal when necessary.

In addition, they can maintain all software packages on the system in widely used formats and keep software installations in one central area.

Like apt-get on Linux, Chocolatey is a command-line package manager designed for Windows. We will use the open-source version of Chocolatey to get the software and tools required for our development environment.

In addition, it will assist you in swiftly installing programs and utilities. For example, we can install Chocolately using the following commands in PowerShell.

Invoke-WebRequest ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) | 
    Invoke-Expression
set "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

While running the snippet above, you may encounter an error that looks like something below:

Invoke-WebRequest: Invalid URI: Hostname could not be parsed.

This error happens if our current network doesn’t trust the URI: https://community.chocolatey.org/install.ps1. As a workaround given by Chocolatey, follow the steps below.

  • Copy the install.ps1 file locally.
  • Open a PowerShell command line.
  • Set the following environment variables.
    $env:chocolateyProxyLocation = 'https://local/proxy/server'
    $env:chocolateyProxyUser = 'username'
    $env:chocolateyProxyPassword = 'password'
    
  • With that same shell open where the environment variables are set, run the downloaded script to install Chocolatey.

The steps commonly fix the issue, but if it persists, you may read more on the provided troubleshooting guide here.

Use Chocolately to Install Python

To install Python, run the following command:

choco install -y python3

Once the process is completed, we can verify our Python installation using its version by running the command below. If it yields an output, we have successfully installed Python using PowerShell.

Example Code:

python --version

#or

py --version

Output:

Python 3.9.0
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn