Thursday, September 24, 2015

Python Hunt Series - Isolated Environment


Python Hunt Series - Isolated Environment 

Prev post in the series - Scaffolding


In this post, we will discuss about the virtual environment option available in Python. Virtual environment creates isolated environment which will provide us the opportunity to install python packages to our convenience without affecting the global python environment

let me give you an use case where virtual environment is must. Consider I am working on 2 projects simultaneously, for my first project I need to use django (web application framework ) version 1.6 and for my second project I need django version 1.8 or greater. In this case I cannot have django 1.6 and >1.8 co existing in the same environment.


The best effective option that I can get is, creating 2 different virtual environment and installing the respective django version over there. Lets see how can we do this 





In order to create virtual environment, we need to install virtualenv package first. Use pip as discussed in first post.


pip install virtualenv

Once we installed virtualenv package, we need to create virtual environment to work with

Type the below command in the terminal to create virtual environment

Virtualenv venv

This will create a virtual environment named "venv" in the current working directory.Get into the venv folder and change directory to bin folder. Now run the below command

Source activate





Now virtual environment venv is active. This you can identify by the environment name appearing at the start of terminal titlebar. By default, pip is available in virtual environment created. Use the same to install dependencies. When I say dependencies it is nothing but the 3 d party packages we need for the current project we are working on

Once we are done with the work, we can deactivate the environment by typing deactivate on the terminal

deactivate

One should run " source activate " again to start working on the environment.

Under the hood, virtual environment will alter the environment variable path to include the binaries from the newly created virtual environment at the start

When we deactivate, the path variable will be rolled back to as it was previously

There is another package called, virtualenvwrapper, this helps in creating, removing, activating and deactivating virtual environment with ease

To install virtualenvwrapper, try the below command

pip install virtualenvwrapper

For linux users, we need to add few environment variable to the .bashrc file under home directory to make virtualenvwrapper work.

Add the below entries at the last to bashrc file

export VIRTUALENVWRAPPER_PYTHON=<path to python binary>
export WORKON_HOME=<path where virtual environments to be created>

source /usr/local/bin/virtualenvwrapper.sh

save the .bashrc file and restart the terminal. Once restarted, try out the below commands

makevirtualenv venv

this will create a new virtual environment under the path set to WORKON_HOME

workon venv

this command will activate the virtual environment

deactivate

this is deactivate the environment

rmvirtualenv venv

this command will remove the virtual environment venv

Create a virtual environment and activate it. Once that is done, then we can start installing dependencies

To install a package of specific version, we need to give the below command

pip install <packagename> == <version>

example 

pip install django==1.8

we can also install multiple dependencies through a text file by giving 

pip install -r requirement.txt

where requirement.txt holds all the packages to be installed with version if needed (each package being specified in new line)

Now we have a working isolated environment with dependencies installed. 

Cheers!

No comments:

Post a Comment