How to install Python virtualenv/virtualenvwrapper?

Virtualenv is a tool which is able to create isolated Python environments. It is mainly used to get rid of problems of dependencies and versions. It also does not include permission setup.

Generally virtualenvwrapper is a kind of extension for virtualenv. It owns wrappers which are in charge of creating and deleting environments. It is useful supplement for our current subject.

In order to install and use virtual enviroments in Linux you have to complete few simple steps.

I used my virtual machine with preinstalled operating system Ubuntu 16.04 to clarify this process but I also attach configuration for Ubuntu 14.04 and Centos 7.

 

1. Log in to your PC and check python/pip version (it is the default information used by virtual environments)

 

eouser@vm1:~$python --version
Python 2.7.12
eouser@vm1:~$ pip -V
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
eouser@vm1:~$

 

If you do not have Python installed on your operating system, please follow those steps for implementation:
eouser@vm1:~$ sudo apt install python-pip
eouser@vm1:~$ sudo apt-get install --reinstall python-pkg-resources

2. Now we have to install some additional packages, which are neccessary to accomplish the installation:

 

  • virtualenv

  • virtualenvwrapper(extension for virtualenv)

 

To get those packages we will use  pip: (use sudo in CentOS):

eouser@vm1:~$ pip install virtualenv

 

And after all:

eouser@vm1:~$ pip install virtualenvwrapper

 

 


3. We downloaded the whole stuff which is the core of our task. Now we have to configure shell file to automate starting virtual enviroments during the start of the system.

 

Use text editor to edit bash file for exporting Paths:

Example 1: for eoconsole in Ubuntu

$ vim ~/.bashrc

 

Example 2: for eouser in Ubuntu

$ vim ~/.profile

 

Example 3: for Centos 7 (both types of user is included)

$ vim ~/.bash_profile

 

 Scroll down at the very end and add three lines which are needed.

For Ubuntu 16.04

export PROJECT_HOME=$HOME/devel
export VIRTUALENVWRAPPER_VIRTUALENV=$HOME/.local/bin/virtualenv
source $HOME/.local/bin/virtualenvwrapper.sh

For Ubuntu 14.04

export PROJECT_HOME=$HOME/devel
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh

For Centos 7

export PROJECT_HOME=$HOME/devel
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/bin/virtualenv
source /usr/bin/virtualenvwrapper.sh

 

Explanations:

The first line tells the shell where the projects will be stored(We have to create this folder with “mkdir ~/.devel” command)

The second line tells the operating system about virtualenv location.

The third line tells the shell to execute virtualenvwrapper.sh during initialization.

 

Save the configuration. Next we are able to apply these changes to current session with source command:

eouser@vm1:~$ source ~/.profile
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/get_env_details

Explanations:

 

source ~/.bashrc (or accordingly source ~/.profile or  source ~/.bash_profile)  command reffered to the virtualenvwrapper.sh executable file and created hidden directory in home/econsole/.virtualenvs where the new enviroments will be located.

 


4. It’s the final step. We might create our first virtual environment. Use mkvirtualenv command and fill in the blank space with the name:

eouser@vm1:~$ mkvirtualenv test1
New python executable in /home/eouser/.virtualenvs/test1/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test1/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test1/bin/preactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test1/bin/postactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/test1/bin/get_env_details
(test1) eouser@vm1:~$

 

From now, everything what is installed by pip command will be stored in directory.

 

For example:

 

~/.virtualenvs/test1

 

If you would like to exit current environment, just type the deactivate command:

 

eouser@vm1:~$ deactivate

 

Important!


Workon command is responsible to change your virtualenv, or choose one if none is selected:

  • Lonely it list all available virtual environment
     

    eouser@vm1:~$ workon
    env35
    project1
    test1

     

  • Written with the name of specific virtualenv e.g workon test1 it manage you to this Python environment
     

    eouser@vm1:~$ workon test1
    (test1) eoconsole@vm1:~$

     


 

To implement python version which differs from the one which we are globally using (2.7.12) we have to add parameter –python=/usr/bin/python3.5

eouser@vm1:~$ mkvirtualenv --python=/usr/bin/python3.5 env35
Running virtualenv with interpreter /usr/bin/python3.5
Using base prefix '/usr'
New python executable in /home/eouser/.virtualenvs/env35/bin/python3.5
Also creating executable in /home/eouser/.virtualenvs/env35/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/env35/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/env35/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/env35/bin/preactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/env35/bin/postactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/env35/bin/get_env_details
(env35) eouser@vm1:~$

 

And the very last thing: We might also create projects with mkproject command:

eouser@vm1:~$ mkproject project1
New python executable in /home/eouser/.virtualenvs/project1/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/project1/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/project1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/project1/bin/preactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/project1/bin/postactivate
virtualenvwrapper.user_scripts creating /home/eouser/.virtualenvs/project1/bin/get_env_details
Creating /home/eouser/devel/project1
Setting project for project1 to /home/eouser/devel/project1