Setting up Python Development Environments

Oct 09, 2016

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.

Python uses pippip for package management.

Installing pip

pippip depends on setuptools library, which is in official Ubuntu repositories. To install it for python2 -

sudo apt-get install python-setuptools
sudo apt-get install python-setuptools

Then install pippip using -

sudo apt-get install python-pip
sudo apt-get install python-pip

and for python3 -

sudo apt-get install python3-setuptools
sudo apt-get install python3-setuptools

Then install pippip using -

sudo apt-get install python3-pip
sudo apt-get install python3-pip

It should install pippip on your system for both python versions. pippip is very easy to use. It will take care of every single package you may require for your project.

Installing a package using pip

# it will search and install [package]
pip install [package]
pip install django
# it will search and install [package]
pip install [package]
pip install django

If you are using python3, then don't forget to use pip3pip3.

pippip can be used to install a specific version of package also.

# it will search and install [package] with [version]
pip install [package]==[version]
pip install django==1.6.5
# it will search and install [package] with [version]
pip install [package]==[version]
pip install django==1.6.5

Uninstalling a package using pip

# it will search and uninstall [package]
pip uninstall [package]
pip uninstall django
# it will search and uninstall [package]
pip uninstall [package]
pip uninstall django

upgrading a package using pip

# it will upgrade [package] to latest version
pip install --upgrade [package]
pip install --upgrade django
# it will upgrade [package] to latest version
pip install --upgrade [package]
pip install --upgrade django

Creating list of all packages with pip

It is one of most used and most useful feature of pippip. It allows you to make a list of all the dependencies of your project.

# it will output the file to current directory
pip freeze > [file_name.txt]
# it will output the file to current directory
pip freeze > [file_name.txt]

All these commands above will install the packages globally. But that's not what is desired. virtualenvvirtualenv comes to our rescue here.

Virtualenv

virtualenvvirtualenv solves a very particular problem; it allows multiple python projects that have different and often conflicting dependencies, to coexist on the same system.

virtualenvvirtualenv 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.

Installing virtualenv

By default, if you install virtualenvvirtualenv using pippip, it will use system's default python to create virtual environments. To overcome this problem, we will install virtualenvvirtualenv using ubuntu package manager.

sudo apt-get install python-virtualenv
sudo apt-get install python-virtualenv

Installing virtualenvwrapper

virtualenvwrappervirtualenvwrapper provides some set of commands which makes working with virtual environments much easier.

To install it -

sudo pip install virtualenvwrapper
sudo pip install virtualenvwrapper

pippip, virtualenvvirtualenv and virtualenvwrappervirtualenvwrapper are the only packages which you will need to install globally. All other per project packages will be installed in respective virtual environments.

virtualenvwrappervirtualenvwrapper also places all your virtual environments in one place. It makes working with projects very easy.

Now open your .bashrc.bashrc and add these two lines to the end -

# All your projects will be saved in python-dev folder
export PROJECT_HOME=~/python-dev
 
# ~/python-dev/virtualenvs will contains python interpreters for each project.
export WORKON_HOME=~/python-dev/virtualenvs
 
# source the virtualenvwrapper script
source /usr/local/bin/virtualenvwrapper.sh
# All your projects will be saved in python-dev folder
export PROJECT_HOME=~/python-dev
 
# ~/python-dev/virtualenvs will contains python interpreters for each project.
export WORKON_HOME=~/python-dev/virtualenvs
 
# source the virtualenvwrapper script
source /usr/local/bin/virtualenvwrapper.sh

You can change python-devpython-dev to any name you wish. Your virtual environments will be created at that location.

Now restart your terminal to source the .bashrc.bashrc or use -

source .bashrc
source .bashrc

Basic Usage

Create a virtual environment -

mkvirtualenv myproject
mkvirtualenv myproject

It will create myprojectmyproject folder in the python-dev directory. To activate this project -

workon myproject
workon myproject

Alternatively you can create project using mkprojectmkproject command. It will create a virtual environment as well as a project directory in the $PROJECT_HOME$PROJECT_HOME, which is cdcd-ed into when you workonworkon myproject.

Don't forget to deactivate current project when you switch between different projects.

To deactivate a project -

deactivate
deactivate

To delete a virtual environment -

rmvirtualenv myproject
rmvirtualenv myproject

List all environments -

lsvirtualenv
lsvirtualenv

it will also list all virtual environments -

workon
workon

Please refer to virtualenvwrapper documentation for full list of virtualenvwrapper commands .

virtualenvwrapper also provides the tab-completion feature which is very handy when you have a lot of projects to work with.

That's it. Hope you liked the post. 😄