Note: A newer version of these instructions is available here
These notes are provided primarily for students at the University of Hamburg (Germany) University of Southampton (United Kingdom) in undergraduate, postgraduate and doctoral studies to help them install Python on their own computers should they wish to do so, and to support their learning of programming, computational science and data science, and subsequently their studies, particular in natural sciences, mathematics, engineering, and computer science.
In short, we suggest to use the Anaconda Python distribution.
By the nature of the information provided, the information is likely to become partially outdated over time. For reference: this mini-introduction was written in September 2016, where Anaconda 4.1 was available, and Python 3.5 is the default Python provided, and revised in March 2019, where Anaconda 2018.12 and Python 3.7.1 were the defaults.
a programming language in which we write computer programs. These programs would be stored in text files that have the ending .py, for example hello.py which may contain:
Python is also
a computer program (the technical term is ''interpreter'') which executes Python programs, such as hello.py. On windows, the Python interpeter is called python.exe and from a command window we could execute the hello.py program by typing:
On Linux and OS X operating systems, the Python interpreter program is called Python, so we can run the program hello.py as:
(This also works on Windows as the operating system does not need the .exe extension.)
For scientific computing and computational modelling, we need additional libraries (so called packages) that are not part of the Python standard library. These allow us, for example, to create plots, operate on matricies, and use specialised numerical methods
The packages we generally need are
- numpy (NUMeric Python): matrices and linear algebra
- pandas: Python data science tools (Series and Dataframes)
- scipy (SCIentific Python): many numerical routines
- matplotlib: (PLOTting LIBrary) creating plots of data
We also use in this training:
- sympy (SYMbolic Python): symbolic computation
- pytest (Python TESTing): a code testing framework
The packages numpy, scipy, pandas and matplotlib are building stones of computational work with Python and widely used.
Sympy has a special role as it allows SYMbolic computation rather than numerical computation.
The pytest package and tool supports regression testing and test driven development -- this is generally important, and particularly so in best practice software engineering for computational studies and research.
Spyder (home page) is s a powerful interactive development environment for the Python language with advanced editing, interactive testing, debugging and introspection features. There is a separate blog entry providing a summary of key features of Spyder, which is also available as Spyder's tutorial from inside Spyder (Help -> Spyder tutorial).
The name SPYDER derives from "Scientific PYthon Development EnviRonment" (SPYDER).
We will use it as the main environment to learn about Python, programming and computational science and engineering.
Useful features include
- provision of the IPython (Qt) console as an interactive prompt, which can display plots inline
- ability to execute snippets of code from the editor in the console
- continuous parsing of files in editor, and provision of visual warnings about potential errors
- step-by-step execution
- variable explorer
Anaconda is one of several Python distributions. Python distributions provide the Python interpreter, together with a list of Python packages and sometimes other related tools, such as editors. To be precise, Anaconda is not limited to packaging Python packages, but initially emerged to cater for Python-based applications and packages.
The packages provide by the Anaconda Python distribution includes all of those that we need, and for that reason we suggest to use Anaconda here.
A key part of the Anaconda Python distribution is Spyder, an interactive development environment for Python, including an editor.
In general, the installation of the Python interpreter (from source/binaries) is fairly straightforward, but installation of additional packages can be a bit tedious.
Instead of doing this manually, we suggest on this page to install the Anaconda Python distribution using these installation instructions, which provides the Python interpreter itself and all packages we need.
The Anaconda Python distribution is available for download for Windows, OS X and Linux operating systems (and free).
For Windows and OS X you are given a choice whether to download the graphical installer or the next based installer. If you don't know what the terminal (OS X) or command prompt (Windows) is, then you are better advised to choose the graphical version. You want to install the default suggestion (Python 3.7, 64bit), not Python 2.
Download the installer, start it, and follow instructions. Accept default values as suggested.
During the installation, you have the option to install Microsoft Visual Studio Code: You don't need to install the Visual Studio Code environment for this course, but it shouldn't do any harm either.
If you are using Linux and you are happy to use the package manager of your distribution -- you will know who you are --, then you may be better advised to install the required packages indivdually rather than installing the whole Anaconda distribution.
(Video run through on Windows: A video summary of downloading Anaconda on Windows, including start up of spyder is available as a video . )
This can be done either by typing spyder in a terminal or inside the Anaconda Prompt, or by starting Spyder through the Anaconda Navigator.
Download the testing file.
Open the file in Spyder via File -> Open.
The execute the file via Run -> Run.
If you get a pop up window, you can accept the default settings and click on the run button.
You should see output similar to this in the lower right window of spyder (you may also see a plot appearing):
Running using Python 3.7.1 (default, Dec 14 2018, 13:28:58) [Clang 4.0.1 (tags/RELEASE_401/final)] Testing Python version-> py3.7 OK Testing numpy... -> numpy OK Testing scipy... -> scipy OK Testing pandas... -> pandas OK Testing matplotlib... -> pylab OK Testing sympy... -> sympy OK Testing pytest... -> pytest OK
If the test program produces these outputs, there is a very good chance that Python and the six listed packages are installed correctly.
Open a console:
- Windows: type cmd in the search box
- Mac OS X: Start the Terminal application that is located in the Utilities folder in Applications
- Linux: start one of the shells you have available, or an xterm or so.
Download the testing file to your machine.
Change directory into the folder you have downloaded the file to, and type:
If all the tests pass, you should see output similar to this:
Running using Python 3.5.1 |Anaconda 4.0.0 (x86_64)| (default, Dec 7 2015, 11:24:55) [GCC 4.2.1 (Apple Inc. build 5577)] Testing Python version-> py3.5 OK Testing numpy... -> numpy OK Testing scipy ... -> scipy OK Testing matplotlib... -> pylab OK Testing sympy -> sympy OK Testing pytest -> pytest OK
If you install Python in other ways than through the Anaconda distribution and, for example, you have only installed the numpy, scipy and matplotlib package, the program's output would be:
Testing numpy... -> numpy OK Testing scipy... -> scipy OK Testing matplotlib... -> pylab OK Testing sympy... Could not import 'sympy' -> fail Testing pytest... Could not import 'pytest' -> fail
To update, for example, spyder and python, follow these steps:
Open a terminal (see step 1 in Running the tests from the console)
Update the conda program (this manages the updating) by typing the following command into the console:
conda update conda
Confirm updates if asked to do so. More than one package may be listed to be updated.
Update individual packages, for example spyder:
conda update spyder
Other packages you may want to update include ipython, ipython-qtconsole and ipython-notebook. The relevant command would be:
conda update ipython ipython-qtconsole ipython-notebook
More details on using the conda package management system is available in the conda documentation page.