How to activate Python virtual environment in PowerShell

It’s quite easy to set up virtual a environment with isolated packages for Python in PowerShell. There are several methods

Method 1. Pipenv

This is the youngest and most convenient method. Pipenv automatically manages the environment and can create virtualenv based on the working directory. Files with virtualenv are stored in a separate directory and the user does not need to worry about them.

Install pipenv package:

Enter the directory where you’d like to work and type:

Pipenv will bootstrap and activate the environment. It will launch subshell. If you’d like to leave the environment simply close the shell by command:

Method 2. Using embedded venv

Python 3 contains package venv. It’s often confused with virtualenv which is a standalone package to manage virtual environments. The advantage of the command is that you do not need to install additional packages. Just create a virtual environment in the directory like pyenv:

Activate the environment:

To deactivate the environment simply call:

Method 3. Good old virtualenv

Creating a virtual environment with virtualenv is an old method. You need to create a directory that will contain the virtual environment and then activate it. virtualenv is not part of the default Python package, it’s necessary to install it:

Now it’s possible to create a new virtualenv in a directory like pyenv by the following command:

The last step is to activate this environment in PowerShell :

You should see the name of your virtual environment in the command line. The virtual environment is active.

To deactivate the environment simply call: