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

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.

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:

  • 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:

  • 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:

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:

To make this the default version of Python, type:

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:

This may take some time to download and install.

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

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

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

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):

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:

  • 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.

Save the script to a file deep_versions.py.

Run the script by typing:

You should see output like:

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.

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

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)

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.


Frustrated With Python Machine Learning?

Master Machine Learning With Python

Develop Your Own Models in Minutes

…with just a few lines of scikit-learn code

Discover 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.

Click to learn more.


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

  1. Mrigank Tiwari March 16, 2017 at 2:11 am #

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

  2. SamarthHalyal March 17, 2017 at 9:53 pm #

    scipy: 0.19.0
    numpy: 1.12.0
    matplotlib: 2.0.0
    pandas: 0.19.2
    statsmodels: 0.8.0
    sklearn: 0.18.1

  3. Phil Salm March 27, 2017 at 1:33 pm #

    I was very excited to find your post because I have had a miserable time trying to get a stable version of Python to work on my Mac. I followed your post above, except installed 3.6.1 since it is the latest. So I substituted ’36’ for ’35’ in all of the commands. Everything worked great until I tried the following command:

    sudo port select –set pip pip36

    I ended up with an error indicating “The specified group ‘pip’ does not exist.’ Any ideas?

    • Phil Salm March 27, 2017 at 3:12 pm #

      I found the issue was that I hadn’t installed pip because that part of the install line above was truncated on the display. Once I did that, I was able to follow the remaining steps without a problem. Once that is all done, any idea where the Python compiler is located? I’d like to use it with PyCharm.

      • Jason Brownlee March 28, 2017 at 8:21 am #

        Glad to here it.

        You can type:

        • Phil Salm March 28, 2017 at 10:48 am #

          Thank you. That did help me locate the correct interpreter for PyCharm to use. But, for some reason when I try to import the keras module, it doesn’t recognize it even after successfully running the install above. In fact, when I try to run that install again, it says it already is installed.

        • Phil Salm March 28, 2017 at 11:35 am #

          It’s all working now. Thank you for your post and your additional help! Time for me to purchase your books!

          • Jason Brownlee March 29, 2017 at 9:05 am #

            I’m very glad to hear you got it working!

    • Jason Brownlee March 28, 2017 at 8:20 am #

      You may need the new path to take effect.

      Consider closing and opening the terminal window or restarting the computer.

  4. Nidhi Sharma April 1, 2017 at 1:42 am #

    Hi, This is great. it worked for me the first time. However, I am trying to install on a second machine.
    My python versions are showing correctly, in spite for below steps.

    1. Install Python version 3.5 using macports. Open a terminal and type:
    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

    Close the terminal window and reopen it.

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

    python -V

    Last command continue to give me Python 2.7.10, even though I have 3.5, 3.6 installed.
    Can you please help.

  5. Ram April 1, 2017 at 10:22 pm #

    scipy: 0.19.0
    numpy: 1.12.1
    matplotlib: 2.0.0
    pandas: 0.19.2
    statsmodels: 0.8.0
    sklearn: 0.18.1

  6. PJ May 29, 2017 at 9:03 am #

    Hello Jason I am getting these errors:

    python versions.py
    scipy: 0.19.0
    numpy: 1.12.1
    matplotlib: 2.0.0
    pandas: 0.20.1
    Traceback (most recent call last):
    File “versions.py”, line 14, in
    import statsmodels
    ImportError: No module named ‘statsmodels’

    sudo pip install statsmodel
    Could not find a version that satisfies the requirement statsmodel (from versions: )
    No matching distribution found for statsmodel

    python deep_versions.py
    Traceback (most recent call last):
    File “deep_versions.py”, line 2, in
    import theano
    ImportError: No module named ‘theano’

    I haven’t found any solutions yet on SO. Any ideas? Thanks,
    PJ.

    • Jason Brownlee June 2, 2017 at 12:19 pm #

      Ouch. You could try skipping statsmodels. It’s not needed for deep learning.

  7. Mayra June 14, 2017 at 1:00 am #

    Thank you Jason! Everything worked out as planned.

    I did everything replacing “35” with “36” , since I’m using python 3.6.

    During the installations I got this message:
    ##############################################################
    # IF YOU ARE USING PYTHON FROM THE TERMINAL, PLEASE INSTALL:
    # py36-readline
    # TO AVOID A LIBEDIT / PYTHON INTERACTION ISSUE.
    # REF: https://trac.macports.org/ticket/48807
    ##############################################################

    I’m assuming I should, right? If so, should the commands be “sudo pip install py36-readline” ?
    Please advice : )

    Thanks again, Mayra

    Here are the versions I got:
    scipy: 0.19.0
    numpy: 1.13.0
    matplotlib: 2.0.2
    pandas: 0.20.2
    statsmodels: 0.8.0
    sklearn: 0.18.1

    theano: 0.9.0
    tensorflow: 1.1.0
    Using TensorFlow backend.
    keras: 2.0.5

    • Jason Brownlee June 14, 2017 at 8:46 am #

      Nice work!

      Sorry, I don’t know about readline, I have not seen this message before.

  8. Pradep June 26, 2017 at 9:29 pm #

    scipy: 0.19.0
    numpy: 1.13.0
    matplotlib: 2.0.2
    pandas: 0.20.2
    statsmodels: 0.8.0
    sklearn: 0.18.2

    theano: 0.9.0
    tensorflow: 1.2.0
    Using TensorFlow backend.
    keras: 2.0.5

  9. Ishmael August 9, 2017 at 11:20 am #

    scipy: 0.19.1
    numpy: 1.13.1
    matplotlib: 2.0.2
    pandas: 0.20.3
    statsmodels: 0.8.0
    sklearn: 0.18.2

Leave a Reply