While leaning web developing using Django and Python, I found out VirtualEnv or Virtual Environment is widely used, in first I didn't get it because I came from Microsoft .NET Framework development platform, so I thought of writing a post about it to make simple.
What is Virtual Environment?
VirtualEnv or Virtual Environment is a way to create isolated development environment each one has it's own packages and specific package version, for example VirEnv1 has Django 1.6 and VirEnv2 has Django 1.7, this isolation will keep your project packages dependency clear, isolated, and as I'll show you below that you can replicate the environment with one command and file, so if you work in team it's easy to share the same environment among the team.
How to install VirtualEnv?
VirtualEnv is a python package so to install is you need to install pip first.
Open terminal and type the following command
sudo apt-get install python-pip
After finish installing pip type the following command to install virtual environment
sudo pip install virtualenv
After finish installing virtualenv, type the following command to create folder to store your virtual environments
How to create virtualenv?
After install virtualenv you are ready to create your first virtual environment, open the terminal:
Browse .virtualenvs folder
Create your virtualenv by tying the following command
Now virtualenv will create a folder inside .virtualenvs with virtualenv name, browse it
Now the virtualenv is created but you need to activate it. To activate the virtualenv type the following command
If you find the treminal current line has virtualenv name inside brackets (VirEnv1) this means that your virtualenv is activated.
There is another package to help you doing the above setups with one simple command which called virtualenvwrapper.
What is virtualenvwrapper?
virtualenvwrapper is a package help creating and managing virtualenv easy and fast.
How to install VirtualWnvWrapper?
To install virtualenvwrapper just type the following command
sudo pip install virtualenvwrapper
How to use virtualenvwrapper?
To use virtualenvwrapper you need first to configure your shell to active its commend every time you open new session.
- Open .bash_profile by typing this command in the terminal nano \~/.bashrc
- Add these 3 lines to the file and save it.
export WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/Develsource /usr/local/bin/virtualenvwrapper.sh
First line configure the path for your virtual environments folder.Second line configure the path for your development project folder.Third line is virtualenvwrapper executable file. virtualenvwrapper Commands:To create new virtual environment:
mkvirtualenv <virtual environment name>
boom your virtual environment is created and activated as well.To activate virtual environment:
workon <virtual environment name>
To deactivate virtual environment:
How to Replicate your virtual environment?
Every virtual environment has its own packages, and there are command to list all the packages and its version, export pacakge list, and install packages from file exported, lets see how to can do this.
List virtual environment packages
Export environment packages
pip freeze > requirements.txt
Install virtual envirment packages from file
pip install -r requirement.txt