November 23, 2016 · ucspython

Running multiple versions of python

You can generally run multiple version of Python minor release 2.7 vs 2.6. These are symlinks created in /usr/bin/.

But how do you go about running multiple versions of the the same minor release - 2.7.9 vs 2.7.10.

I wrote a small script that downloads, builds python from source and installs it in a path of its own.

The below is to be run with the required version, for e.g.2.7.9.

#!/bin/bash
version=$1
mkdir ~/python
wget http://www.python.org/ftp/python/$version/Python-$version.tgz
tar -zxvf Python-$version.tgz
cd Python-$version
mkdir ~/.localpython-$version
./configure --prefix=$HOME/.localpython-$version
make
make install

It can then be used from ~/.localpython-2.7.9/bin/python.

we can now set up a virtual environment specifically for each version of python we have installed.

Install virtualenv

sudo apt-get install virtualenv

Add the following to ~/.bashrc or ~/.zshrc,

source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME="$HOME/virtual_env/"
export PIP_VIRTUALENV_BASE=$WORKON_HOME

Now you can create a new environment using the below command. The -p option specifies the python version to use for this environment. --no-site-packages will restrict this environment from using the python libraries from the global environment.

mkvirtualenv --no-site-packages -p ~/.localpython-2.7.9/bin/python python279

Here on, we call switch to this new environment using the below,

workon python279

Can get out this environment by using

deactivate
  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket