Skip to content

Virtualenv and Python

RUGANO edited this page Jan 14, 2014 · 1 revision

Installing Python

The recommended python version is 2.7.3 or higher but not 3 which is still under heavy testing and many of our projects have not yet been adopted to it.

http://www.python.org/getit/releases/2.7.3/

For linux, it normally comes pre-installed, but sudo apt-get install python or similar will work 2.x is safer to use with RapidSMS, to ensure no strange backwards-compatibility issues with 3.x.

To find out the version of python that is running on your system

python –-version

Easy installing easy_install

Easy install is basic package management for python. The other is pip, which comes pre-loaded with virtualenv see below if you are not familiar with virtualenv otherwise, easy_install can be found here: http://peak.telecommunity.com/dist/ez_setup.py

wget “http://peak.telecommunity.com/dist/ez_setup.py” 
python ez_setup.py
easy_install --help

You may need to install wget before the above instructions can work

sudo apt-get install wget

Virtual Environment

Virtualenv creates isolated python environments, complete with their own interpreter and site-packages directory Virtualenvs or virtual environments are especially useful for projects requiring different versions of the same package but are on the same or are supposed to be run on the same computer platform.

To install;

easy_install virtualenv

Virtual Environment – the bad

It's complicated to activate them. Specifically, it requires a long structure for activating a particular virtual environment: src env/bin/activate

Its difficult to tell tell from a shell in which virtual environment is activated and each virtual environment lives in a different place on the file system, usually attached to a project

Virtualenvwrapper

Virtualenvwrapper tries to respond to some of the challenges of virtualenv outlined above i.e.

-Organizes all virtual environments in one place -Wrappers for creating, copying and deleting environments -Single command to switch between environments -Easy-to-read indicator of which environment is currently acitve

Virtualenvwrapper setup

Install
easy_install virtualenvwrapper
Create a root folder for your environments
mkdir /path/to/my/virtualenvs

Add it to your .profile or for Linux actualy it is added in the file .bashrc

export WORKON_HOME=/path/to/my/virtualenvs
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true   #to make pip detect any virtual env when used 
source /usr/local/bin/virtualenvwrapper.sh
mkvirtualenv

Creates a new virtual environment as a clone of your /usr/python

mkvirtualenv training
workon

Activates an environment

workon # list all envs
workon training # activate an env
deactivate

Deactivates the current environment

deactivate
lssitepackages

This is a Shortcut for listing the site packages within a particular environment

lssitepackages

Interactive shell and debugger

Some times there are mistakes in the code or the code can not run for one reason or another, an interactive shell and debugger is great for tab completion and other useful features. Its also dependency of shell_plus, a utility for Django

easy_install ipython
easy_install ipdb
Debugger
‘l’ist # used to show the context of the code that is being run
‘n’ext # used to go to the next line in the code
‘c’ontinue # used to continue parsing the code to the end
‘s’tep into # used to step into the code i.e stepping out of the current code and into a dependent function or module
‘q’uit # used to quit the interactive debugger
‘bt’ backtrace # backtrace

All regular python commands (including changing the value of a variable!)