Newby Coder header banner

Python Venv

virtualenv

Virtual Environments (also called "sandboxes") can be thought of as a separate Python installation for different projects using different versions of the same package(or dependency) without conflicting with one another

For example, there might be a project/program made in Python 2.7 and another with Python 3

Keeping these projects under separate Virtual Environments can keep their dependencies isolated, preventing possible conflicts if not done so, while also not affecting the system versions outside these environments

It can also be of help when user doesn't have adequate permissions for installing in system directories

Installing Python virtualenv

Install virtualenv with the following command:

$ pip install virtualenv 

Or for python 3

$ pip3 install virtualenv 

Installing Python virtualenv in Linux/ Windows/ Mac OS

Execute command provided above

Creating Virtual Environment

Activate virtualenv:

Unix:

$ source env/bin/activate 

Windows:

$ env\scripts\activate 

Create a new directory using mkdir command and use cd command to change current working directory to the new directory

$ mkdir test
$ cd test

Although this isn't necessary step, it can be made a habit to created directories for separate projects/programs

If using Python 2.7, use virtualenv command, with a name for the virtual env, to create a virtual environment

 virtualenv nc_venv 

or alternatively :

python -m virtualenv nc_venv

Command to create virtualenv with Python 3

To create virtualenv in Python 3, use venv module with name of env to be created such as

 python -m venv nc_venv 

This creates a new directory named nc_venv in current working directory

The structure of the virtual env directory (here nc_env), for Python 2.7, should look like this:

env/
β”œβ”€β”€bin/
β”œβ”€β”€include/
β”œβ”€β”€lib/
└──pip-selfcheck.json

For Python 3, nc_venv looks like

env/
β”œβ”€β”€bin/
β”œβ”€β”€include/
β”œβ”€β”€lib/
β”œβ”€β”€lib64/
β”œβ”€β”€pyvenv.cfg
└──share/

These files and folder represent a separate Python installation

The bin/ folder contains the executables that can be found in an ordinary Python installation:

Activating the Virtual Environment

To use a virtual environment, it has to be activated

In Linux/Mac OS, virtual environment can be activated with the following command:

 source env/bin/activate 
cl-python-venv-activate

Activating Python virtual env on Windows

To activate virtual env on Windows, use the following command:

 env\bin\activate 

Output

C:\Users\nc>env\bin\activate
(nc_venv)C:\Users\nc> 

Virtual environment is now active indicated by (nc_venv)(or whatever name provided as virtual env name) appearing in front of the shell(or terminal) prompt

Activating a virtual environment changes the $PATH environment variable temporarily by adding the bin/ directory of the virtual environment to it

Usage of python command executes the Python executable of env/bin/ directory instead of the globally installed Python

Also, any package added or removed using pip only affects the virtual environment

Deactivating a Virtual Environment

A Virtualenv can be deactivated using the deactivate command:

 deactivate 

This removes the bin/ directory of virtual environment from the $PATH environment variable, making globally installed Python accessible again