Sunday, 8 November 2015

Installation of Django in Virtual Environment

This post will lead you through successful installation of Django in Virtual Environment on Debian based system like Ubuntu. Here we will create virtual environment using most popular and recommended tool virtualenv and virtualwrapper.

What is Virtual Environment?
Virtual environment is a isolated Python environment that keeps python files in a isolation manner from system's python files and other virtual environment's python files.

Some important note about virtual environment: 
  • If you install any program using apt-get repository from virtual environment (created by virtualenv or virtualwrapper) then it will get install on whole system.
  • To install python package only inside virtual environment, use pip or easy_install like python package managers.

Why to install Django in virtual environment?
After installing Django you will create several projects using it. It may happen that your two projects have same dependency but requires different version. And on a particular system, you can have only single version of any file. Now what??
Solution is Virtual Environment. Python virtual environment solves this problem easily because virtual environment keeps the dependencies required by different projects in separate places. It keeps your global site-packages directory clean and manageable.

What is virtualenv?
Virtualenv is a tool to cretae isolated Python environments. virtualenv creates a folder which contains all the executables you installed inside the virtual environment.

Now, what is virtualwrapper?
virtualenvwrapper is just a wrapper utility around virtualenv that makes easy to deal with virtual environments. This post will cover working with virtualenv via virtualwrapper to provide ease.
virtualenvwrapper includes set of shell functions that are guaranteed to work in the following shells:
bash (Bourne again shell)
ksh (Korn shell)
zsh (Z shell)

Good news for windows lover, virtualwrapper also has window version that is virtual-wrapper-win.

Installation Procees 
1. Before any installation, it is a best practice to update our packages so run:

sudo apt-get update

2. We will install virtaulenv and virtualwrapper using pip (pyhton index package). Hence first you need to install pip. For it use following command:

sudo apt-get -y install python-pip

3. Install virtualenv:

pip install virtualenv

4. Install virtualwrapper:

pip install virtualenvwrapper

Now you have installed virtualenv and virtualwrapper tool. Now you need to do some congifugration setting. Lets walk through that.

Configuration of virtualwrapper

To use virtualwrapper, you need to add following two lines in your shell startup file, most probably .bash_profile or .bashrc. Lets do it. First open shell startup file using your faviourite editor and add:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/

The first line instructs virtualenvwrapper where to store the virtualenvs that will be created by you. Here we are storing it in a folder called .virtualenvs inside your home folder. The second line runs the shell script to set up the virtualenvwrapper commands. It should point to the location where virtualenvwrapper is installed.

Now you have done with installation and configuration process. Its time to play with it. Here are some basic commands, you need to create and manage virtual environments:
  • mkvirtualenv – It is used to create a new virtual environment. Created new environment automatically becomes the active environment. 
  • rmvirtualenv – It is used to remove an existing virtual environment. The environment must be deactivated (explained below) before it can be removed. 
  • workon – It is used to activate a virtual environment. Will also list all existing virtual environments if no argument is passed. 
  • deactivate - used to deactivate the currently active virtual environment. Note that workon will automatically deactivate the current environment before activating a new one.

Django In Virtual environment

1. Lets create virtual enviroment first. So run:
mkvirtualenv test

2. Now tou are in virtual environment. You can identify it by seeing test in round bracket at left side of your terminal. Now install Djnago:
pip install django

Now you have successfully installed Django in virtual environment. Thankyou for reading.

No comments:

Post a Comment