Solarian Programmer

My programming ramblings

Install Python 3 with NumPy, SciPy and Matplotlib on macOS High Sierra

Posted on October 4, 2016 by Sol

Updated 21 April 2018

In this article, I will show you how to install Python 3 with NumPy, SciPy and Matplotlib on macOS High Sierra. As an experiment, I’ve asked our partner AtoZ Programming Tutorials to record a video of the written tutorial. The video and the written tutorial overlap for the install part, however the written tutorial has different usage examples.

Please leave some feedback in the comments section about the mixed video and text approach from a teaching perspective. If the experiment is successful, we will continue to create more mixed tutorials.

MacOS comes by default with Python 2.7 which, at this point, receives only bug fixes and will be EOL by 2020. Python 3.x is the future and it is supported by all major Python libraries. In this tutorial, we’ll use the latest stable Python 3 release, Python 3.6.4.

There is more than one way to install Python 3 on macOS. From my experience, the easiest approach for beginners is to use a package manager like Miniconda. Select the 3.6 bash installer from the Miniconda download page. Once the download is finished, open a Terminal and launch the installer:

1 cd Downloads/
2 bash Miniconda3-latest-MacOSX-x86_64.sh

For the most part, you can use the defaults suggested by the installer. Be careful when asked if you want Miniconda added to your PATH. If you chose yes, the Miniconda Python will shadow the system Python, so when you will write python in a Terminal you will launch Python 3.6 instead of the default 2.7. My advice is to add Miniconda to your PATH. If, at some point, you want to revert to 2.7, it is as simple as commenting the Miniconda line from your .bash_profile file.

Once the installation is finished, assuming you kept the installer defaults, you need to enable the new PATH settings. This can be achieved by closing and reopening your Terminal, or simply by writing:

1 cd ~
2 . .bash_profile

Now, you should have the conda command available in your PATH. A quick test is to run the conda info command. This is what I see in my case:

 1 ~ $ conda info
 2 
 3      active environment : None
 4        user config file : /Users/paul/.condarc
 5  populated config files :
 6           conda version : 4.4.10
 7     conda-build version : not installed
 8          python version : 3.6.4.final.0
 9        base environment : /Users/paul/miniconda3  (writable)
10            channel URLs : https://repo.continuum.io/pkgs/main/osx-64
11                           https://repo.continuum.io/pkgs/main/noarch
12                           https://repo.continuum.io/pkgs/free/osx-64
13                           https://repo.continuum.io/pkgs/free/noarch
14                           https://repo.continuum.io/pkgs/r/osx-64
15                           https://repo.continuum.io/pkgs/r/noarch
16                           https://repo.continuum.io/pkgs/pro/osx-64
17                           https://repo.continuum.io/pkgs/pro/noarch
18           package cache : /Users/paul/miniconda3/pkgs
19                           /Users/paul/.conda/pkgs
20        envs directories : /Users/paul/miniconda3/envs
21                           /Users/paul/.conda/envs
22                platform : osx-64
23              user-agent : conda/4.4.10 requests/2.18.4 CPython/3.6.4 Darwin/17.5.0 OSX/10.13.4
24                 UID:GID : 501:20
25              netrc file : None
26            offline mode : False
27 ~ $

Let’s follow best practices and create a new Python environment:

1 conda create -n myenv python=3.6
2 source activate myenv

At this point, your prompt should indicate that you are using the myenv environment. An environment allows you to have different versions of Python and libraries on the same machine. As an example, you could have a myenv environment where you’ve installed SciPy and a play environment where you’ve installed PyGame. The environments are completely separate from each other. This is useful if you want, for example, to experiment with the development version of Python or some other library, while keeping the stable versions separate.

Once an environment is activated, all the install commands will apply only to the current environment. By default, if you close your Terminal, the environment is deactivated. If you want to be able to use it, use the source activate myenv command.

We can install NumPy, SciPy and Matplotlib with:

1 conda install numpy scipy matplotlib

Fire up Python, import scipy and print the version of the installed library. This is what I see on my machine:

1 (myenv) ~ $ python
2 Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:14:23)
3 [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
4 Type "help", "copyright", "credits" or "license" for more information.
5 >>> import scipy as sp
6 >>> sp.version.version
7 '1.0.1'
8 >>>

If you want to see an example of working with matrices, check the Youtube video.

Let’s try something a bit more interesting now, let’s plot a simple function with Matplotlib. First, we’ll import SciPy and Matplotlib with:

1 import scipy as sp
2 import matplotlib.pylab as plt

Next, we can define some points on the (0, 1) interval with:

1 t = sp.linspace(0, 1, 100)

Now, let’s plot a parabola defined by the above interval:

1 plt.plot(t, t**2)
2 plt.show()

You should see something like this:

Matplotlib parabola

As you’ve probably noticed, plt.show() is a blocking command. You won’t be able to use the interpreter until you close Figure 1.

There is also an interactive mode in which you can plot functions. Close Figure 1 and write:

1 plt.ion()
2 plt.plot(t, t**2)
3 plt.title("Interactive plot test")
4 plt.plot(t, t**3)
5 plt.xlabel("t")

This is what you should see:

Matplotlib interactive mode draw two curves, add title and axis labels

At any point you can disable the interactive plot mode with:

1 plt.ioff()

after which you will need to use the plt.show() function in order to actually see the result of the plt.plot function.

If you want to learn more about Python and Matplotlib, I recommend reading Python Crash Course by Eric Matthes. The book is intended for beginners, but has a nice Data Visualization intro to Matplotlib chapter:

Another good Python book, for more advanced users, which also uses Matplotlib for some of the book projects is Python Playground by Mahesh Venkitachalam:


Show Comments