This article is for absolute begginers in using Virualenv and Virtualenvwrapper. If you know how to use them already, you will feel bored. When I started to use Python, I always wondered how I can use different versions of a package in my system. Later I came to know about Virtualenv which can be used for creating multiple development environments in python.
Virtualenv is the package with which you can create mutilple isolate python development environments. Every every will have their own site-packages and environment variables.
Step 1.1: Install virtualenv
Install Virtualenv using the command
sudo pip install virtualenv
Step 1.2: Create virtual environment
Create virtualenv using the command
virtualenv venv_name --no-site-packages
Step 1.3: Activate virtual environment
You can activate the virtualenv using the command
Now you can see that virtual environment is activated by seeing
(venv_name) at the beggining of your bash prompt.
After this point, all the python packages you install will be installed in the virtual environment instead of your system's main python installation. Like this you can create multiple virtual environments and install multiple versions of the same package in each virtual environment.
But we are not done yet. There are some limitations and rules in using this. You have to be in the virtual environment directory to create and activate virtual environments.
Installing virtualenvwrapper will get you past the limitations mentioned above in using virtualenv. As the name reads, it is a package built on top of virtualenv to make it easier to work with virtualenv.
Step 2.1: Install virtualenvwrapper
Install virtualenvwrapper using the command
sudo pip install virtualenvwrapper
Step 2.2: Edit bash config
Open your bash config file and add the following lines in the file
export WORKON_HOME=~/venvs source /usr/local/bin/virtualenvwrapper.sh
Here WORKON_HOME is the location where all the virtualenvs you create with virtualenvwrapper will be stored. You can change it to a folder of your choice and make sure that the folder exists. Then run the following command to activate the changes made in bash config file.
source ~/.bashrc #In case of Ubuntu source ~/.bash_profile #In case of OS X
Step 2.3: Create virtual environment
You can create virtual environment using the command
You don't need to worry about the directory from which you run this command. All the virtualenvs will be created in the directory you defined in
WORKON_HOME in the previous step.
Step 2.4: Activate virtual environment
You can activate virtual environment using the command
You don't have to remember the location of virtual environments. This will command will activate the environment.
These are the basic features of virtualenvwrapper. You can do lot more which I will add to this post soon. Feel free to post your comments.