Recently I was searching for Python projects on Github for contribution. Every
single project I found, had a thing common among them. In every project's
contribution guide, it was asked to set up the virtual environment for the
project. What the heck is this virtual environment and how does it work?
As a beginner to open source projects, the problem I faced, in the beginning,
was how to set up the development environments for the projects I was looking
at. I searched the Internet, I found some articles, but they were not complete.
So I decided to write this guide, which will be useful for me in future also.
pip depends on setuptools library, which is in official Ubuntu repositories.
To install it for python2 -
bash
sudo apt-get install python-setuptools
Then install pip using -
bash
sudo apt-get install python-pip
and for python3 -
bash
sudo apt-get install python3-setuptools
Then install pip using -
bash
sudo apt-get install python3-pip
It should install pip on your system for both python versions. pip is very
easy to use. It will take care of every single package you may require for your
project.
virtualenv solves a very particular problem; it allows multiple python
projects that have different and often conflicting dependencies, to coexist on
the same system.
virtualenv solves this problem by creating different isolated development
environments for your projects. An environment is a folder which contains
everything; your project needs to work properly.
By default, if you install virtualenv using pip, it will use system's
default python to create virtual environments. To overcome this problem, we will
install virtualenv using ubuntu package manager.
virtualenvwrapper provides some set of commands which makes working with
virtual environments much easier.
To install it -
bash
sudo pip install virtualenvwrapper
pip, virtualenv and virtualenvwrapper are the only packages which you will
need to install globally. All other per project packages will be installed in
respective virtual environments.
virtualenvwrapper also places all your virtual environments in one place. It
makes working with projects very easy.
Now open your .bashrc and add these two lines to the end -
bash
# All your projects will be saved in python-dev folderexport PROJECT_HOME=~/python-dev# ~/python-dev/virtualenvs will contains python interpreters for each project.export WORKON_HOME=~/python-dev/virtualenvs# source the virtualenvwrapper scriptsource /usr/local/bin/virtualenvwrapper.sh
You can change python-dev to any name you wish. Your virtual environments will
be created at that location.
Now restart your terminal to source the .bashrc or use -
It will create myproject folder in the python-dev directory. To activate this
project -
bash
workon myproject
Alternatively you can create project using mkproject command. It will create a
virtual environment as well as a project directory in the $PROJECT_HOME, which
is cd-ed into when you workon myproject.
Don't forget to deactivate current project when you switch between different
projects.