Contents
Mac doesn’t have a preinstalled package manager, but there are a couple of popular package managers you can install. For Python 3.5 with Macports, execute this command in a terminal: sudo port install py35 - numpy py35 - scipy py35 - matplotlib py35 - ipython + notebook py35 - pandas py35 - sympy py35 - nose. Installing Matplotlib on your Mac or Windows Laptop. First install Python 3.x: Even if you have an older version of Python, e.g., Python 3.2, on your laptop, you can still install a newer version, e.g., 3.5 or 3.6. On your Windowsmachine download and install the latest Python 3 Releasefrom Python. I know it seems fairly redundant to have a title for a legend, but is it possible using matplotlib? Here's a snippet of the code I have: import matplotlib.patches as mpatches import matplotlib.py.
This is the most recent version of the installationinstructions. (Older versions from 2014/2013, where we have used Python 2 (!) are available here.)
These notes are provided primarily for students of graduate schools IMPRS and DASHH, staff and students at the Max Planck Institute for the Structure and Dynamics of Matter and others at DESY, as well as students at the University of Southampton (United Kingdom).
The objective of these introductory notes is to help readers install Python ontheir own computers, and to support their learning of programming, computationalscience and data science, and subsequently their studies, particular in naturalsciences, mathematics, engineering, and computer science.
In short, we suggest to use theAnaconda Python distribution.
By the nature of the information provided, the content is likelyto become partially outdated over time. For reference: thismini-introduction was written in September 2016, where Anaconda 4.1was available, and Python 3.5 is the default Python provided, andrevised in March 2021, where Anaconda 2020.11 and Python 3.8 werethe defaults.
Python is a programming language in which we write computer programs. Theseprograms are stored in text files that have the ending .py, for examplehello.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 programis called Python, so we can run the program hello.py as:
(This also works on Windows as the operating system does not needthe .exe extension.)
For scientific computing and computational modelling, we needadditional libraries (sometimes called packages) that are not part of thePython standard library. These allow us, for example, to create plots,operate on matricies, and use specialised numerical methods.
The packages we often need include are
We also use in this training:
The packages numpy, scipy, pandas and matplotlib are essential components computational work with Python and widely used.
Sympy has a special role as it allows SYMbolic computationrather than numerical computation.
The pytest package and tool supports regression testing and testdriven development -- this is generally important, and particularly soin best practice software engineering for computational studies andresearch.
Spyder (home page) is s a powerfulinteractive development environment for the Python language withadvanced editing, interactive testing, debugging and introspectionfeatures. There is a separate blog entry providing asummary 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
Anaconda is aPython distributions. Python distributions provide the Pythoninterpreter, together with a list of Python packages and sometimesother related tools, such as editors. To be more precise, Anaconda is notlimited to packaging Python packages, but initially emerged to caterfor Python-based applications and packages.
The packages provide by the Anaconda Python distribution include 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, aninteractive development environment for Python, including an editor.
In general, the installation of the Python interpreter (fromsource/binaries) is fairly straightforward, but installation ofadditional packages can be a bit tedious.
Instead of doing this manually, we suggest on this page to install the AnacondaPython distribution using these installation instructions, which provides thePython interpreter itself and all packages we need.
The Anaconda Python distribution is available for download forWindows, OS X and Linux operating systems (and free).
For Windows and OS X you are given a choice whether to download thegraphical installer or the next based installer. If you don't knowwhat the terminal (OS X) or command prompt (Windows) is, then you arebetter advised to choose the graphical version.
If you are using Linux, you probably want what is called 'Linux' and not what is'Linux POWER'. The 'Linux' target refers to the common x86 architecture.
Download the installer, start it, and follow instructions. Acceptdefault values as suggested.
During the installation, you may have the option to install additional editingenvironments. You don't need to install these for this course, but it shouldn'tdo any harm either.
If you are using Linux and you are happy to use the package managerof your distribution -- you will know who you are --, then you may bebetter advised to install the required packages indivdually ratherthan installing the whole Anaconda distribution.
Once you have installed Anaconda or the Python distribution of yourchoice, you can download a testing program andexecute it.
Start Spyder
This can be done either by typing spyder in a terminal orinside the Anaconda Prompt, or by starting Spyder through theAnaconda Navigator.
The current version of Spyder is 4.1.
Spyder may ask you if you want to install kite. This is not necessary forthe course.
Download thetesting 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 andclick on the run button.
You should see output similar to this in the lower right window ofspyder (you may also see a plot appearing):
If the test program produces these outputs, there is a very goodchance that Python and the six listed packages are installedcorrectly.
Open a console:
Download the testing fileto 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:
If you install Python in other ways than through the Anacondadistribution and, for example, you have only installed the numpy,scipy and matplotlib package, the program's output would be:
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 typingthe following command into the console:
Confirm updates if asked to do so. More than one package may belisted to be updated.
Update individual packages, for example spyder:
This introductory page from the Anaconda team may contain useful material to getstarted with the Anaconda.
If you prefer a video run through of an anaconda installation, checkSteve Holden's post from June 2015
To lean more about Anaconda, try the documents and introductory tutorialsoffered at https://docs.anaconda.com/anaconda/ .
I was trying to install matplotlib in Mac OSX Lion. Tried to used the binary that is in the sourcefourge site, but I got this error: “matplotlib requires System Python 2.7 to install”.
I went to the Terminal and typed python –version and its check the requirements.
After this I tried to used the pip, and while doing so é got the following error:
“src/_image.cpp:908: error: invalid use of incomplete type ‘struct png_info_def’”
Can you give me clues in how to install it?
Many thanks.
EDIT: I found a way, without having to install a new version of python:
http://the.taoofmac.com/space/blog/2011/07/24/2222
You can try with an “official” python distribution, apple might have tweaked the supplied one. You can find the 2.7 here: http://www.python.org/download/
You might have to re-install numpy as well afterwords: http://sourceforge.net/projects/numpy/files/NumPy/1.6.1/
I would suggest to install scipy as well
Let me know if it works 😉
This is because of a change in the API for libpng v1.5 (which is included with Mac OSX Lion). The changes has already been pushed upstream, so you will need to compile from the upstream repository until the next release.
You can follow the instructions on http://jholewinski.wordpress.com/2011/07/21/installing-matplotlib-on-os-x-10-7-with-homebrew/
I followed this page’s instructions. I got stuck at
Then I did:
Checked my installation by typing in terminal:
I got version 0.10.0 dev (as of this writing) and path /usr/local/Cellar/…
I would recommend using macports, it should take care of dependencies and would be indepedent of the system python version.
EDIT: Just a few clarifications, taking into account comments to this answer.
Why use macports (or another installer)? Because they take care of dependencies, provide functionality to uninstall and switch between versions (I’ve used the latter successfully for gcc and python). And because the default installation location is not the system executable location. Overriding the system python can break applications that rely on it (this is certainly true in many Linux distributions, maybe less so on mac OS X).
When is it particularly useful? When you want to install on top of a version of python that is different to system python, and when you have non-python extensions (C, C++, Fortran…).
What’s the down side? As @Trond has mentioned in the comments, it is good if you’re OK with default compilations of packages. You don’t have a handle over configuration or compiler flags as you would if you built from source.
A good alternative to macports is fink, which seems to give you more control (build from source). I am not sure it is completely ready for mac OS X Lion yet.
I know it will take a lot more time, but I would reccomend installing python, numpy, and matplotlib from source to ensure that you have consistency in your system. From the error it could look like you have a problem with libPNG support. I have a written a “How to install python dev tools on Mac OSX” that you may find useful. It contains directions for how to install python, numpy, matplotlib and all necessary dependencies. I understand you are working with Python 2.7 and the directions I am listing is for Python 2.6.5, but it would be the same approach. Hope you get it to work.
You could just install the Python 2.6 version of Matplotlib, assuming that Python2.6 is included with Lion (py2.5 & 2.6 are included with Mac OS 10.6 “Snow Leopard” – try typing python2.6 to find out if that version is installed.).
On MacPorts you do this via something like:
where py26-… (or py25-…, py27-… etc.) indicates which python version it is meant for. (check the Pallet/ports list to get the correct package name, in case I guessed wrong)