Python development setup

Pip

Pip is a package manager for Python packages. The list of more common packages can be found on Python Package Index (PyPI).

To install or remove a package you just need to do

$ pip install <package_name>
$ pip uninstall <package_name>

A cool feature coming with Pip is an easy way to manage “requirements” through a simple list (package name with versions). You can extract that and use it to replicate the environment with these commands:

$ pip freeze > requirements.txt
$ pip install -r requirements.txt

VirtualEnv

The cool feature this tools gives you it’s the ability to manage completely separated environments to develop in a non-messy environment. This also helps when you got to keep different versions of Python and libs for different projects.

To install it just do

$ pip install virtualenv

To create a new virtual environment just do

$ virtualenv TEST

That command will create a local folder with inside everything need to manage your new separated world. To use that (to install packages in it and run your project inside it) you should do

$ source bin/activate
(TEST) $

With the activate command your shell environment has been manipulated to let you run your python project within this new separated space. Your $PATH has been manipulated to have this virtualenv’s bin/ as first entry and your prompt now displays the name of the environment you’re in.

Now you can go on and install whatever you need with pip.

While inside your new environment your prompt will be modified to display in which environment you are (the “(TEST)” part).

and to exit it just and restore your shell environment just do

(TEST) $ deactivate

VirtualEnvWrapper

This project exploits VirtualEnv adding some other cool stuff and make the day to day work so much easier. The best things you got with this tool are:

  • virtual environments all stored in a single place, not spread around like before
  • wrapper to create, delete, copy and activate virtual environments with tab completion (!)

To install and setup it just do

$ sudo pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/bin/virtualenvwrapper.sh

and insert following lines in .bash_rc to have this tools ready next time too

$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh

Now that it’s installed let’s create a new environment

$ mkvirtualenv env1
New python executable in env1/bin/python
Installing setuptools, pip...done.
(env1) $

As you can see that command will also activate the environment. Now you can install whatever package you want with pip as usual. To list all installed packages you can use lssitepackages command.

To switch to another environment or to activate one just use workon (you can use tab to invoke the list of available environments or just issue the command alone to get a plain list)

$ workon
env1
env2
env3
$ workon (press tab)
env1      env2      env3
$ workon env3
(env3)$

To delete a virtual environment just do

$ rmvirtualenv env1

A full list of command can be found here.

ipython

IPython (capital ‘i’) it’s a powerful interactive shells that we can use within our preferred IDE (eg Eclipse+PyDev) to get cool features like tab completion (eg for attribute navigation), object exploration, magic functions, etc.

To install it and invoke it just do

$ pip install ipython
$ ipython
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
Type "copyright", "credits" or "license" for more information.

IPython 2.2.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]:

References

  • http://virtualenv.readthedocs.org/en/latest/
  • http://mrcoles.com/tips-using-pip-virtualenv-virtualenvwrapper/
  • http://virtualenvwrapper.readthedocs.org/en/latest/
  • http://ipython.org/documentation.html