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:

python3 -m pip install pipenv

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

pipenv shell

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:

exit

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:

python3 -m pyenv 

Activate the environment:

.\pyenv\Scripts\activate.ps1

To deactivate the environment simply call:

deactivate

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:

python3 -m pip install virtualenv

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

virtualenv pyenv

The last step is to activate this environment in PowerShell :

.\pyenv\Scripts\activate.ps1

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

To deactivate the environment simply call:

deactivate

26. May 2013 at 12:48 - Software engineering (Tags: , , , ). Both comments and pings are currently closed.

Comments are closed.