How to run Python3 Virtualenv in PowerShell

Last Updated on April 26, 2021

Below is a step-by-step instruction on how to run Python3 Virtualenv in PowerShell.

The tutorial below assumes that you have already installed python3 and pip3 in your windows computer.

Installation of virtualenv in Python3

In PowerShell, install virtualenv using python pip.

pip3 install virtualenv

Create a virtual environment

virtualenv venv

Error when activating newly created virtual environment

To activate the the Virtual Environment that you have created, you will need to run the command activate.ps1 script inside the Scripts folder. If this is your first time activating virtualenv it will lead to the UnathorizedAccess error File activate.ps1 cannot be loaded because running scripts is disabled on this system.

Click on the image for a bigger photo

By default PowerShell does not allow running of ps1 scripts. To fix this we will need to allow our user to run PowerShell scripts.

Open PowerShell as Administrator and run the command below. This will enable running of ps1 in PowerShell.

Set-ExecutionPolicy RemoteSigned

It will then warn you that changing the execution policy might expose you to some security risks. Type y and press Enter.

Allowing user to run scripts in PowerShell

Now that we have enabled running scripts in PowerShell, activate your virtual environment by running the command below.

./virtualenv_name/Scripts/activate.ps1

This will put a (virtualenv_name) before your prompt, this indicates that your virtual environment has been activated.


Deactivating Virtualenv

To deactivate your virtual environment run the command below.

deactivate

Disable running PowerShellScripts

I usually do not do this since I regularly need running virtualenv, but if you want you can revert back to disabling running PowerShell scripts.

To do this, run the command below using PowerShell running as Administrator.

Set-ExecutionPolicy Restricted

We hope that the above helped you run virtualenv in PowerShell.

Leave a Reply

Your email address will not be published. Required fields are marked *