Python 2.7 and Virtual Env setup

I need to get a custom version of Python installed - I generally use 3.3 - however I presently need to work in Apache Spark http://spark.apache.org. The python module I wish to use is PySpark - however this is only available as a 2.7 egg.

Get The Source

In order to do a custom build - we need to source code.

This can be found at https://www.python.org/download/releases/2.7/ and the command

    cd ~/Dev
    mkdir Python27
    cd Python27
    wget https://www.python.org/ftp/python/2.7/Python-2.7.tgz
    tar -zxvf Python-2.7.tgz

Configure

We now need to create the Makefiles - but before that stage we need to run Configure - so we build Python and it will link to the correct Modules.

   cd ~/Dev/Python27/Python2-7
   ./configure  

The configure script should now end

configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Modules/Setup.config
config.status: creating Misc/python.pc
config.status: creating pyconfig.h
creating Modules/Setup
creating Modules/Setup.local
creating Makefile

This is a successful state - if you do not see this - look at where errors have occured, it should be obvious what package it is looking for at the time. You need to correct the package error (usually by installing the missing libraries) - and then re-run ./configure.

Build

With the Makefiles now having been created - we need to compile the Python2-7 code

   cd ~/Dev/Python/Python2-7
   make

You again need to check the end of this process - I saw the following.....

changing mode of build/scripts-2.7/idle from 644 to 755
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python.exe-gdb.py

Install

You can now install the Python2-7, becuase we did not specify and specific directory to install into - this will default to /usr/local/ whic is a good place.

   cd ~/Dev/Python/Python2-7
   make install

You make need special privs to do this step in which case you need to

 su root
 make install

Virtual Env for Python 2.7

Slightly briefer explanation this time.... Get the Package from PIP and instal to the Python2.7 SYSTEM Image (If you are unable to do this - then give yourself a private copy of Python 2.7 - and install to that).

cd ~/src
wget https://pypi.python.org/packages/source/v/virtualenv/virtualenv-13.0.3.tar.gz#md5=cd2043ea72448d65dcc54d33744c2214
tar -zxvf virtualenv-13-0.3
cd virtualenv-13-0.3
/usr/local/bin/python setup.py install

Create a VirtualEnv

We now should be able to create a virtual Environment for Python2-7

virtualenv ~/pe27 -p /usr/local/bin/python2.7

Add Alias

To make this easier to switch I add an alias

All the line alias venv27='source ~/pe27/bin/activate' to the file ~/.bashrc

Either source ~/.bashrc or close and re-open a terminal

You should be able to switch python environemtns by typing pe27