Introduction

With one Python installation, it may not be possible to meet the requirements of every application. One application may needs version 1.0 of a particular module but other application may needs version 2.0 of this module. In this case requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment. A virtual environment is self-contained directory tree that contains a Python installation for a particular version of Python, and other additional packages. It allow Python site packages (third party libraries) to be installed locally in an isolated directory for a particular project, as opposed to being installed globally (i.e. as part of a system-wide Python). Different applications can then use different virtual environments. So application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0.

Creating Virtual Environment

venv module is used to create and manage virtual environments. To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:

# Content of test-env directory
# test-env
# ├── src
# └── tests

cd test-project/
python3 -m venv tutorial-env 	# Creates an environment called tutorial-env/

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. Now the content of directory look like this:

# Content of test-env directory
# test-env
# ├── src      
# ├── tests    
# └── tutorial-env

Once you’ve created a virtual environment, execute below command to activate virtual environment

tutorial-env\Scripts\activate.bat  # Applicable on Windows
source tutorial-env/bin/activate   # Applicable On Unix or MacOS

Now virtual environment is set for running the application. Project is completely isolated from the rest of our system. Inside virtual environment system-wide site packages are not accessible. Also any packages we install in virtual environment will not be accessible outside. To exit the environment, enter below command

deactivate

Managing Packages

Pip can be used to install, upgrade, and remove packages. It has a number of subcommands, such as install, uninstall, freeze, etc. To install a package, execute below command

(tutorial-env) $ python -m pip install requests   		# Install latest version of a package
(tutorial-env) $ python -m pip install requests==2.6.0		# Install a specific version of a package by giving the package name followed by == and the version number.

You can run pip install --upgrade to upgrade the package to the latest version. pip uninstall followed by one or more package names will remove the packages from the virtual environment. To display information about a particular package, run below

(tutorial-env) $ pip show requests  # Show information about specific package
(tutorial-env) $ pip list	    # Display all of the packages installed in the virtual environment:

pip freeze will produce a list of the installed packages, but the output uses the format that pip install expects. A common convention is to put this list in a requirements.txt. This file can be shipped as part of an application, other users can then install all the necessary packages with install -r.

# Dump the installed packages in file
(tutorial-env) $ pip freeze > requirements.txt

# Show the content of file
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

# Installing package from requirements.txt
(tutorial-env) $ python -m pip install -r requirements.txt