/    /  Python – Virtualenv

Python – Virtualenv

In this tutorial you will learn python in detail.

 

Python applications may use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library or the application may be written using an obsolete version of the library’s interface.

Consider a case if project1 needs version 1.0 of a particular module but project2 needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one project unable to run.

In order to resolve this problem we need to create a virtual environment. This virtual environment creates self-contained directory for python projects, where they need their own dependencies, packages and versions they need.  

 

Creating virtual Environments:

 

If you are using Python3 version, then it is not needed to install virtualenv. Otherwise you’ll need to install the virtualenv tool with pip. 

For this type below command in the command prompt.

 

$ pip install virtualenv

 

To work with it start by making a new directory.

 

$ mkdir environments

 

Go to this directory by giving below command

 

$ cd environments

 

Now create a new virtual environment inside this directory as shown below.

 

# Python 2
$ virtualenv project1_env
# Python 3
$ python3 -m venv project1_env

 

The above command creates project1_env directory and also creates directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.

Once after creating a virtual environment, you need to activate it. Type below command to activate.

 

$ source project1_env/bin/activate

 

How to know this is activated?  you can see that the shell’s prompt changes showing what virtual environment you are using.

To see the path type below command in the command prompt:

 

$ which python

 

To see the list of packages, present in the virtual environment type below command:

 

$ pip list

 

Here we will have only pip and setup tools, no other global packages.

Here we can install packages required by specifying a package’s name. For example:

 

(Project1_env) $ pip install novas
Collecting novas
Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
Running setup.py install for novas
Successfully installed novas-3.1.1.3

 

If you want to install a specific version of a package, give the package name followed by == and the version number. Example shown below:

 

(project1_env) $ pip install requests==2.6.0
Collecting requests==2.6.0
Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0

 

If you try to re-run above command, pip will notice that the requested version is already installed and do nothing. You can upgrade to the latest version of that package by using pip install –upgrade   or you can supply a different version number to get that version. Example as shown below:

 

(project1_env) $ pip install --upgrade requests
Collecting requests
Installing collected packages: requests
Found existing installation: requests 2.6.0
   Uninstalling requests-2.6.0:
     Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0

 

To remove one or more packages names from the virtual environment, use pip uninstall command.

 

To display information about a particular package use pip show, as shown below:

 

(project1_env) $ pip show requests
---
Metadata-Version: 2.0
Name: requests
Version: 2.7.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache 2.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-
packages
Requires:

 

Leaving the virtual environment

 

If you want to leave your virtual environment or otherwise want to switch projects, simply run

$ deactivate