Install Python on Windows With Virtual Environments

Janne Kemppainen |

Today I wanted to install Python natively on my Windows machine. Nowadays it is super easy as python can be installed straight from the Microsoft Store. So if you need to have Python on Windows I really recommend that you go to the Microsoft Store from the Start menu, search for Python and install it from there.

Run Python

Run Python

To verify the installation open a new PowerShell window and type python. The Python interpreter should run showing you the installed version (I installed Python 3.8.0).

Install Python from Windows Store

Install Python from Windows Store

The recommended way to create virtual environments in Python is to use the built-in venv module like this:

python -m venv venv

The first “venv” means the venv module and the second one is just the path of the virtual envrionment, you can use any path you want.

After creating the virtual environment you need to use the Activate.ps1 script to enable it on PowerShell instead of sourcing venv/bin/activate like you'd do on Linux and Mac.

.\venv\Scripts\Activate.ps1

However, I encountered an error that says “Activate.ps1 cannot be loaded because running scripts is disabled on this system.” This happens because PowerShell doesn't have permission to run scripts and as a result the UnauthorizedAccess error is thrown.

.\venv\Scripts\Activate.ps1 : File J:\Päkstech\hugo-word\venv\Scripts\Activate.ps1 cannot be loaded because running scripts is
disabled on this system. For more information, see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\venv\Scripts\Activate.ps1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
Execution policy error message

Execution policy error message

To fix this error you need to set the execution policy to allow scripts. Open a new PowerShell window with “run as administrator” and use one of these two commands:

  • Set-ExecutionPolicy RemoteSigned
  • Set-ExecutionPolicy Unsestricted

These are the descriptions from the Microsoft documentation page:

RemoteSigned. Requires that all scripts and configuration files downloaded from the Internet are signed by a trusted publisher.

Unrestricted. Loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs. The default execution policy for non-Windows computers and cannot be changed.

Now you should be able to activate the virtual environment without errors.

Virtual environment working again

Virtual environment working again

That's the quick and easy way to install Python for Windows.

Subscribe to my newsletter

What's new with PäksTech? Subscribe to receive occasional emails where I will sum up stuff that has happened at the blog and what may be coming next.

powered by TinyLetter | Privacy Policy