Virtual Environment in Python – A Pocket Guide

how to create virtual environments

In almost every article, we recommended the use of virtual environment for developing any Python or Django project.

In this article, we will briefly cover the virtual environment in python, installation and usage.

What is a Virtual Environment:

Virtual environment is an isolated python environment which can be created using virtualenv python tool. This virtual environment contains all the packages that a python package would require. Python project running in virtual environment does not use the system wide installed python package.

 


Installing Virtual Environment:

We can install virtual environment using pip.

You can confirm the installation by running below command.


Using virtual environment:

Create a virtual environment:

This will create a virtual environment with name project_1_env  and install python binaries in this folder. This will use the system’s default python’ version.

To create virtual environment with specific python version use below command.

or

To work inside a virtual environment, we need to activate it.

Now you may install python packages inside this isolated environment using pip install command.

Once you are done working with your project, you may deactivate the virtual environment by deactivate  command.

 


Deleting virtual environment:

To delete a virtual environment simply remove the directory.


Freezing the requirements:

When you deploy your project on any other server like PythonAnyWhere, you need to replicate the virtual environment. You can not simply copy the folder as it will break the things. Also this is not a good idea as the size of the folder will be large.

We will first list all the packages installed in virtual environment and dump in a file.

Now commit this file along with your code.

On production server, clone the code repository and create a virtual environment. Once virtual environment is activated, install all the dependencies using the pip install command below.

This will create the exact copy of virtual environment you were working on development server.

 



Pipenv:

Pipenv is a dependency manager for python projects. It is similar to Symfony’s composer and node.js’s npm. Its a higher lever tool than pip.

We recommend to use pipenv for managing your virtual environments.

With pipenv, we need not to manage requirement.txt file separately.

As per pienv readthedocs:

It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever–important Pipfile.lock, which is used to produce deterministic builds.

Install pipenv using pip.

 


Creating virtual environment using pipenv:

Go to your project directory.

Install python packages using pipenv command.

Your new virtual environment is created at location  /home/rana/.local/share/virtualenvs/django_project_1-NloTdiFm  as you can see in the output above.

It will create the Pipfile.lock  in current directory just like compose.lock  in symfony projects (PHP coder can relate) which is a replacement of requirement.txt file and can be used to replicate the virtual environment.


Now you can activate the virtual environment created in above step using below command.

deactivate the environment using exit  command.


Other commands:

pipenv graph:

Generates a dependency tree structure showing what all packages are required for a package.

Example:

virtual environment in python

 

pipenv install –dev:

Installs all dev dependencies.

 

pipenv –venv:

Give the location of virtual environment.

Example:

virtual environment location

 

pipenv uninstall –all:

Uninstall everything. Environment is purged and fresh as in starting.

 


Using Pipfile and Pipfile.lock:

To replicate the virtual environment on another server, you can reinstall the dependencies using below commands.

It will use the Pipfile  located in current directory.

To use Pipfile.lock  file to install dependencies use below command.

 

Go through the references provided below to learn about the pipenv.

References:
[1] http://pipenv.readthedocs.io/en/latest/
[2] http://docs.python-guide.org/en/latest/dev/virtualenvs/

(Visited 372 times, 1 visits today)

You must read this :