Install Python 3 with NumPy, SciPy and Matplotlib on macOS Sierra
Posted on October 4, 2016 by Sol
In this article, I will show you how to install Python 3 with NumPy, SciPy and Matplotlib on macOS 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.5.2.
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.5 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.5 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 Current conda install: 3 4 platform : osx-64 5 conda version : 4.1.11 6 conda-env version : 2.5.2 7 conda-build version : not installed 8 python version : 3.5.2.final.0 9 requests version : 2.10.0 10 root environment : /Users/sol/miniconda3 (writable) 11 default environment : /Users/sol/miniconda3 12 envs directories : /Users/sol/miniconda3/envs 13 package cache : /Users/sol/miniconda3/pkgs 14 channel URLs : https://repo.continuum.io/pkgs/free/osx-64/ 15 https://repo.continuum.io/pkgs/free/noarch/ 16 https://repo.continuum.io/pkgs/pro/osx-64/ 17 https://repo.continuum.io/pkgs/pro/noarch/ 18 config file : None 19 offline mode : False 20 is foreign system : False 21 22 ~ $
Let’s follow best practices and create a new Python environment:
1 conda create -n myenv python=3.5 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.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:52:12) 3 [GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin 4 Type "help", "copyright", "credits" or "license" for more information. 5 >>> import scipy as sp 6 >>> sp.version.version 7 '0.18.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:
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:
At any point you can disable the interactive plot mode with:
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: