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 just go open the Microsoft Store from the Start menu, search for Python and then just install the version you want.

Install Python from Windows Store

Install Python from Windows Store

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.

Install Python from Windows Store

Install Python from Windows Store

To fix this error you need to set the execution policy to allow scripts. Open a new PowerShell window with “run as administrator” and enable scripts with the command:

Set-ExecutionPolicy Unrestricted

This is the description from the Microsoft documentation page:

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.

Install Python from Windows Store

Install Python from Windows Store

So there you have it, a quick and easy Python installation 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