Adapted by John Jasa (johnjasa@umich.edu) from work by J.R. Johansson (jrjohansson at gmail.com)
Python is a modern, general-purpose, object-oriented, high-level programming language.
General characteristics of Python:
Technical details:
Advantages:
Disadvantages:
Python has a strong position in scientific computing:
Extensive ecosystem of scientific libraries and environments
Great performance due to close integration with time-tested and highly optimized codes written in C and Fortran:
Good support for
Readily available and suitable for use on high-performance computing clusters.
No license costs, no unnecessary use of research budget.
The standard way to use the Python programming language is to use the Python interpreter to run python code. The python interpreter is a program that reads and execute the python code in files passed to it as arguments. At the command prompt, the command python
is used to invoke the Python interpreter.
For example, to run a file my-program.py
that contains python code from the command prompt, use::
$ python my-program.py
We can also start the interpreter by simply typing python
at the command line, and interactively type python code into the interpreter.
This is often how we want to work when developing scientific applications, or when doing small calculations. But the standard python interpreter is not very convenient for this kind of work, due to a number of limitations.
IPython is an interactive shell that addresses the limitation of the standard python interpreter, and it is a work-horse for scientific use of python. It provides an interactive prompt to the python interpreter with a greatly improved user-friendliness.
Some of the many useful features of IPython includes:
IPython notebook is an HTML-based notebook environment for Python, similar to Mathematica or Maple. It is based on the IPython shell, but provides a cell-based environment with great interactivity, where calculations can be organized and documented in a structured way.
Although using a web browser as graphical interface, IPython notebooks are usually run locally, from the same computer that run the browser. To start a new IPython notebook session, run the following command:
$ ipython notebook
from a directory where you want the notebooks to be stored. This will open a new browser window (or a new tab in an existing window) with an index page where existing notebooks are shown and from which new notebooks can be created.
Spyder is a MATLAB-like IDE for scientific computing with python. It has the many advantages of a traditional IDE environment, for example that everything from code editing, execution and debugging is carried out in a single environment, and work on different calculations can be organized as projects in the IDE environment.
Some advantages of Spyder:
There are currently two versions of python: Python 2 and Python 3. Python 3 will eventually supercede Python 2, but it is not backward-compatible with Python 2. A lot of existing python code and packages has been written for Python 2, and it is still the most wide-spread version. For these lectures either version will be fine, but it is probably easier to stick with Python 2 for now, because it is more readily available via prebuilt packages and binary installers.
To see which version of Python you have, run
$ python --version
Python 2.7.3
$ python3.2 --version
Python 3.2.3
Several versions of Python can be installed in parallel, as shown above.
The best way set-up an scientific Python environment is to use the cross-platform package manager conda
from Continuum Analytics. First download and install miniconda http://conda.pydata.org/miniconda.html or Anaconda (see below). Next, to install the required libraries for these notebooks, simply run:
$ conda install ipython ipython-notebook spyder numpy scipy sympy matplotlib cython
This should be sufficient to get a working environment on any platform supported by conda
.
In Ubuntu Linux, to installing python and all the requirements run:
$ sudo apt-get install python ipython ipython-notebook
sudoapt−getinstallpython−numpypython−scipypython−matplotlibpython−sympy sudo apt-get install spyder
Macports
Python is included by default in Mac OS X, but for our purposes it will be useful to install a new python environment using Macports, because it makes it much easier to install all the required additional packages. Using Macports, we can install what we need with:
$ sudo port install py27-ipython +pyside+notebook+parallel+scientific
$ sudo port install py27-scipy py27-matplotlib py27-sympy
$ sudo port install py27-spyder
These will associate the commands python
and ipython
with the versions installed via macports (instead of the one that is shipped with Mac OS X), run the following commands:
$ sudo port select python python27
$ sudo port select ipython ipython27
Fink
Or, alternatively, you can use the Fink package manager. After installing Fink, use the following command to install python and the packages that we need:
$ sudo fink install python27 ipython-py27 numpy-py27 matplotlib-py27 scipy-py27 sympy-py27
$ sudo fink install spyder-mac-py27
Windows lacks a good packaging system, so the easiest way to setup a Python environment is to install a pre-packaged distribution. Some good alternatives are:
EPD and Anaconda are also available for Linux and Max OS X.