Install Python on Windows With Virtual EnvironmentsJanne 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.
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).
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.
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.
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:
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.
So there you have it, a quick and easy Python installation for Windows.