Skip to content
Search
Generic filters
Exact matches only

How to Install a Python for Machine Learning on macOS

Last Updated on August 21, 2019

It can be difficult to install a Python machine learning environment on Mac OS X.

Python itself must be installed first, and then there are many packages to install, and it can be confusing for beginners.

In this tutorial, you will discover how to setup a Python 3 machine learning and deep learning development environment using macports.

After completing this tutorial, you will have a working Python 3 environment to begin learning, practicing, and developing machine learning and deep learning software.

Discover how to prepare data with pandas, fit and evaluate models with scikit-learn, and more in my new book, with 16 step-by-step tutorials, 3 projects, and full python code.

Let’s get started.

  • Update Aug/2017: Added a section on how to keep your environment up to date.

How to Install a Python 3 Environment on Mac OS X for Machine Learning and Deep Learning

How to Install a Python 3 Environment on Mac OS X for Machine Learning and Deep Learning

Tutorial Overview

This tutorial is broken down into the following 4 steps:

  1. Install XCode Tools
  2. Install Macports
  3. Install SciPy Libraries
  4. Install Deep Learning Libraries
  5. Keep Your Environment Up-to-Date

1. Install XCode

XCode is the IDE for development on OS X.

Installation of XCode is required because it contains command line tools needed for Python development. In this step, you will install XCode and the XCode command line tools.

This step assumes you already have an Apple App Store account and that you have sufficient administrative privileges to install software on your workstation.

  • 1. Open the “App Store” application. Search for “XCode” and click the “Get” button to install.

You will be prompted to enter your App Store password.

XCode is free and is at least 4.5 GB in size and may take some time to download.

App Store Search for XCode

App Store Search for XCode

  • 2. Open “Applications” and then locate and start “XCode“.

You may be prompted with a message to install additional components before XCode can be started. Agree and install.

Install Additional XCode Components

Install Additional XCode Components

  • 3. Install the XCode Command Line Tools, Open a terminal window and type:

A dialog will appear and install required tools.

Confirm the tools are installed by typing:

You should see output like:

/Applications/Xcode.app/Contents/Developer

/Applications/Xcode.app/Contents/Developer

  • 4. Agree to the license agreement (if needed). Open a terminal window and type:

Use the “space” key to navigate to the bottom and agree.

You now have XCode and the XCode Command Line Tools installed.

2. Install Macports

Macports is a package management tool for installing development tools on OS X.

In this step, you will install the macports package management tool.

  • 1. Visit macports.org
  • 2. Click the “Download” button at the top of the page to access the install page.
  • 3. Download the “macOS Package (.pkg) Installer” for your version of OS X.

At the time of writing, the latest version of OS X is Sierra.

Macports Package Installation

Macports Package Installation

You should now have a package on your workstation. For example:

MacPorts-2.3.5-10.12-Sierra.pkg

MacPorts-2.3.5-10.12-Sierra.pkg

  • 4. Double click the package and follow through the wizard to install macports.

Macports Installation Wizard

Macports Installation Wizard

  • 5. Update macports and confirm the system is working as expected. Open a terminal window and type:

This will update the port command and the list of available ports and is useful to do from time to time.

You should see a message like:

MacPorts base is already the latest version

MacPorts base is already the latest version

3. Install SciPy and Machine Learning Libraries

SciPy is the collection of scientific computing Python libraries needed for machine learning development in Python.

In this step, you will install the Python 3 and SciPy environment.

  • 1. Install Python version 3.5 using macports. Open a terminal and type:

sudo port install python35

sudo port install python35

To make this the default version of Python, type:

sudo port select –set python python35
sudo port select –set python3 python35

sudo port select –set python python35

sudo port select –set python3 python35

Close the terminal window and reopen it.

Confirm that Python 3.5 is now the default Python for the system by typing:

You should see the message below, or similar:

  • 2. Install the SciPy environment, including the libraries:
    • NumPy
    • SciPy
    • Matplotlib
    • Pandas
    • Statsmodels
    • Pip (package manager)

Open a terminal and type:

sudo port install py35-numpy py35-scipy py35-matplotlib py35-pandas py35-statsmodels py35-pip

sudo port install py35-numpy py35-scipy py35-matplotlib py35-pandas py35-statsmodels py35-pip

This may take some time to download and install.

To ensure pip for Python 3 is the default for the system, type:

sudo port select –set pip pip35

sudo port select –set pip pip35

  • 3. Install scikit-learn using pip. Open the command line and type:

sudo pip install -U scikit-learn

sudo pip install -U scikit-learn

  • 4. Confirm the libraries were installed correctly. Open a text editor and write (copy-paste) the following script:

# scipy
import scipy
print(‘scipy: %s’ % scipy.__version__)
# numpy
import numpy
print(‘numpy: %s’ % numpy.__version__)
# matplotlib
import matplotlib
print(‘matplotlib: %s’ % matplotlib.__version__)
# pandas
import pandas
print(‘pandas: %s’ % pandas.__version__)
# statsmodels
import statsmodels
print(‘statsmodels: %s’ % statsmodels.__version__)
# scikit-learn
import sklearn
print(‘sklearn: %s’ % sklearn.__version__)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

# scipy

import scipy

print(‘scipy: %s’ % scipy.__version__)

# numpy

import numpy

print(‘numpy: %s’ % numpy.__version__)

# matplotlib

import matplotlib

print(‘matplotlib: %s’ % matplotlib.__version__)

# pandas

import pandas

print(‘pandas: %s’ % pandas.__version__)

# statsmodels

import statsmodels

print(‘statsmodels: %s’ % statsmodels.__version__)

# scikit-learn

import sklearn

print(‘sklearn: %s’ % sklearn.__version__)

Save the script with the filename versions.py.

Change directory to the location where you saved the script and type:

The output should look like the following (or similar):

scipy: 0.18.1
numpy: 1.12.0
matplotlib: 2.0.0
pandas: 0.19.2
statsmodels: 0.6.1
sklearn: 0.18.1

scipy: 0.18.1

numpy: 1.12.0

matplotlib: 2.0.0

pandas: 0.19.2

statsmodels: 0.6.1

sklearn: 0.18.1

What versions did you get?
Paste the output in the comments below.

You can use these commands to update machine learning and SciPy libraries as needed.

Try a scikit-learn tutorial, such as:

4. Install Deep Learning Libraries

In this step, we will install Python libraries used for deep learning, specifically: Theano, TensorFlow, and Keras.

  • 1. Install the Theano deep learning library by typing:
  • 2. Install the TensorFlow deep learning library by typing:

sudo pip install tensorflow

sudo pip install tensorflow

  • 3. To install Keras, type:
  • 4. Confirm your deep learning environment is installed and working correctly.

Create a script that prints the version numbers of each library, as we did before for the SciPy environment.

# theano
import theano
print(‘theano: %s’ % theano.__version__)
# tensorflow
import tensorflow
print(‘tensorflow: %s’ % tensorflow.__version__)
# keras
import keras
print(‘keras: %s’ % keras.__version__)

# theano

import theano

print(‘theano: %s’ % theano.__version__)

# tensorflow

import tensorflow

print(‘tensorflow: %s’ % tensorflow.__version__)

# keras

import keras

print(‘keras: %s’ % keras.__version__)

Save the script to a file deep_versions.py.

Run the script by typing:

You should see output like:

theano: 0.8.2
tensorflow: 0.12.1
Using TensorFlow backend.
keras: 1.2.1

theano: 0.8.2

tensorflow: 0.12.1

Using TensorFlow backend.

keras: 1.2.1

What versions did you get?
Paste the output in the comments below.

Try a Keras deep learning tutorial, such as:

5. Keep Your Environment Up-to-Date

It is important to keep your environment up to date over time.

It is also important to use the same tools to update your libraries that were used to install the, e.g. macports and pip.

This section provides commands you can use, say once per month, to ensure that your environment is up to date.

Update Libraries Installed With Macports

The first step is to update macports itself.

Next, you can update the libraries that you installed using macports.

sudo port upgrade python35 py35-numpy py35-scipy py35-matplotlib py35-pandas py35-statsmodels py35-pip

sudo port upgrade python35 py35-numpy py35-scipy py35-matplotlib py35-pandas py35-statsmodels py35-pip

You can also update all libraries that need an update by typing:

sudo port upgrade outdated

sudo port upgrade outdated

I don’t do this myself as I don’t have control over what is being updated.

Update Libraries Installed With Pip

Next, we can update the libraries installed with pip.

I don’t want pip to install or update things that can be installed with macports, so I update libraries installed with pip without updating dependencies (e.g. –no-deps)

sudo pip install -U –no-deps keras tensorflow theano scikit-learn

sudo pip install -U –no-deps keras tensorflow theano scikit-learn

And that’s all you need to do to keep your environment up to date.

If you get some crossover between macports and pip, (e.g. numpy installed by both tools), you can get problems.

To see what exactly is installed with pip type:

Further Reading

This section provides some resources for further reading.

Summary

Congratulations, you now have a working Python development environment on Mac OS X for machine learning and deep learning.

You can now learn and practice machine learning and deep learning on your workstation.

How did you do?
Let me know in the comments below.

Discover Fast Machine Learning in Python!

Master Machine Learning With Python

Develop Your Own Models in Minutes

…with just a few lines of scikit-learn code

Learn how in my new Ebook:
Machine Learning Mastery With Python

Covers self-study tutorials and end-to-end projects like:
Loading data, visualization, modeling, tuning, and much more…

Finally Bring Machine Learning To
Your Own Projects

Skip the Academics. Just Results.

See What’s Inside

error: Content is protected !!