Davide Gerosa

Caltech

The best way to install Python

Python is cool, especially if you a scientist. All sort of scientific algorithms are already there (written and debugged!), you just have to use them.

This is a step-by-step guide to the best way I found to install and use python for science, the easy (less hackable) way comes first, and the long way comes next.

The easy way on mac: homebrew

Most (if not all) unix systems already come with a python distribution installed. However, it is advisable to install a local distribution and do your scientific stuff from there… If you screw something up, you can just delete everything and your OS is safe. I will also stay with python 2, as everything still works (see here for more information on the issue).

The easiest way to safely install python on a MAC is homebrew. Homebrew installs a new version of python (by default the latest 2.x version available) and set is as default.

brew install python

Next, we want a virtualenv. Python’s virtual environments are kind of separate boxes, where you can install modules and packages locally. You can have different boxes for different projects, or a single box for all your python stuff. Again, if you screw something up, you can just delete the box and start again. Now type

pip install virtualenv
virtualenv ~/box

The virtual environment is physically located in ~/box. You have created the box, but you’re still out of it. To get into the box

chmod u+x ${HOME}/box/bin/activate
source ${HOME}/box/bin/activate

Now you’re in the box, and you should see “(box)” close to your username in the terminal. To get out of the box, type

deactivate

and the label “(box)” should disappear from your terminal.

You may want to add this last command in your .bashrc

echo "" >>${HOME}/.bashrc
echo "alias inthebox='source ${HOME}/localpython/box/bin/activate' " >> ${HOME}/.bashrc
source ${HOME}/.bashrc

Now type “inthebox” and “deactivate” to get in and out of your new virtual environment.

The hard way (for the PROs)

Homebrew is great, but you can of course do the same manually. The procedure below is somehow taken from this stack overflow question. Instructions here are given again for mac OSX  but can be easily generalized to any unix system (e.g. replacing curl with wget and so on…).

First let’s install a python distribution. Go to  this URL:

https://www.python.org/ftp/python/

and check what is the latest version of python 2. At the time of writing, this is 2.7.13, change the following lines if you want a different version. I am going to assume you want to put your new python distribution in a directory called “localpython” in your home directory

cd
mkdir localpython
cd localpython
curl  https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz > Python-2.7.10.tgz
tar -zxvf Python-2.7.13.tgz
cd Python-2.7.13

# "make clean" may be necessary here for earlier versions
./configure --prefix=${HOME}/localpython --enable-optimizations
make
make install

If, at any time, you need to start again, just

rm -rf ${HOME}/localpython

and you should be fine.

Now, virtualenv. Go to this URL:

https://pypi.python.org/pypi/virtualenv

and check what is the latest version of virtualenv. At the time of writing, this is 15.1.0, change the following lines if you want a different version.

cd ${HOME}/localpython
curl  https://pypi.python.org/packages/source/v/virtualenv/virtualenv-15.1.0.tar.gz > virtualenv-15.1.0.tar.gz
tar -zxvf virtualenv-15.1.0.tar.gz
cd virtualenv-15.1.0
${HOME}/localpython/bin/python setup.py install

Update: unfortunately pypi.python.org changed their link structure and the link above doesn’t work anymore. You need to go that webpage with a browser and get the tar.gz manually. At the time of writing, even the virtualenv official documentation has not been updated yet.

Now we create a virtual environment specifying it should run python from our local distribution. Again, we call our virtual environment “box” and place it in localpython

cd ${HOME}/localpython 
${HOME}/localpython/bin/virtualenv box --python=${HOME}/localpython/bin/python

If you get an error involving zlib, this is likely to be related to an upgrade of the OS: get back to the homebrew solution. If you’re still alive, type

chmod u+x ${HOME}/localpython/box/bin/activate
source ${HOME}/localpython/box/bin/activate

You can test everything with

which python
>>>  ${HOME}/localpython/box/bin/python

If you enter a python console, you should get today’s date and time (or yesterday’s if you found these instructions exhausting)

python
>>>  Python 2.7.10 (default, TODAY! )

To get out of the box, type

deactivate

As before, we can add these commands to our .bashrc

echo "" >>${HOME}/.bashrc
echo "alias inthebox='source ${HOME}/localpython/box/bin/activate' " >> ${HOME}/.bashrc
source ${HOME}/.bashrc

Science is fun again: install packages

Now you’re done. Virtualenv comes with pip, the tool to install python modules from the Python Package Index PyPi. From within your box, to install a package type

pip install PACKAGE_NAME

First of all, upgrade pip itself. It’s not needed strictly, but my experience is that it may fix issues, especially on mac OS X

pip install --upgrade setuptools pip

You can try with numpy, scipy, matploltib, and my own precession module to study black holes.

Update: no pip, no fun!

I recently got across the situation that I did not want to use a virtual environment. This was because I was on a supercomputer which already had a python distribution installed and I didn’t want to deal with the scipy dependancies (e.g. lbpack etc.).

So, how to get pip and install modules anyway? The python people have a script precisely for this:

wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py --user

Note the –user flag, which is good if you can’t sudo (as it was for me on the Caltech supercomputer).  Now, you only need to remember to install modules using this slightly different line:

python -m pip install MODULENAME --user

Done! Enjoy python on your supercomputer.

Now you can fly

Seriously… just import antigravity

python

Credit: xkcd n. 353

Picture: building tall stuff in Southampton, UK