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.

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

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:

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.

15 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

Leave a Reply