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.
Kick-start your project with my new book Machine Learning Mastery With Python, including step-by-step tutorials and the Python source code files for all examples.
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
Tutorial Overview
This tutorial is broken down into the following 4 steps:
- Install XCode Tools
- Install Macports
- Install SciPy Libraries
- Install Deep Learning Libraries
- 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
- 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
- 3. Install the XCode Command Line Tools, Open a terminal window and type:
1 |
xcode-select --install |
A dialog will appear and install required tools.
Confirm the tools are installed by typing:
1 |
xcode-select -p |
You should see output like:
1 |
/Applications/Xcode.app/Contents/Developer |
- 4. Agree to the license agreement (if needed). Open a terminal window and type:
1 |
sudo xcodebuild -license |
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
You should now have a package on your workstation. For example:
1 |
MacPorts-2.3.5-10.12-Sierra.pkg |
- 4. Double click the package and follow through the wizard to install macports.

Macports Installation Wizard
- 5. Update macports and confirm the system is working as expected. Open a terminal window and type:
1 |
sudo port selfupdate |
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:
1 |
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:
1 |
sudo port install python35 |
To make this the default version of Python, type:
1 2 |
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:
1 |
python -V |
You should see the message below, or similar:
1 |
Python 3.5.3 |
- 2. Install the SciPy environment, including the libraries:
- NumPy
- SciPy
- Matplotlib
- Pandas
- Statsmodels
- Pip (package manager)
Open a terminal and type:
1 |
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:
1 |
sudo port select --set pip pip35 |
- 3. Install scikit-learn using pip. Open the command line and type:
1 |
sudo pip install -U scikit-learn |
- 4. Confirm the libraries were installed correctly. Open a text editor and write (copy-paste) the following script:
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:
1 |
python versions.py |
The output should look like the following (or similar):
1 2 3 4 5 6 |
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:
1 |
sudo pip install theano |
- 2. Install the TensorFlow deep learning library by typing:
1 |
sudo pip install tensorflow |
- 3. To install Keras, type:
1 |
sudo pip install keras |
- 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.
1 2 3 4 5 6 7 8 9 |
# 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:
1 |
python deep_versions.py |
You should see output like:
1 2 3 4 |
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.
1 |
sudo port selfupdate |
Next, you can update the libraries that you installed using macports.
1 |
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:
1 |
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)
1 |
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:
1 |
sudo pip freeze |
Further Reading
This section provides some resources for further reading.
- MacPorts Installation
- Chapter 2. Installing MacPorts
- Chapter 3. Using MacPorts
- Installing the SciPy Stack
- Scikit-learn Installation
- Installing Theano
- Install TensorFlow Anaconda
- Keras Installation
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.
scipy: 0.18.1
numpy: 1.11.3
matplotlib: 2.0.0
pandas: 0.19.2
statsmodels: 0.6.1
sklearn: 0.18.1
Very nice Mrigank.
scipy: 0.19.0
numpy: 1.12.0
matplotlib: 2.0.0
pandas: 0.19.2
statsmodels: 0.8.0
sklearn: 0.18.1
Very nice work!
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?
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.
Glad to here it.
You can type:
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.
It’s all working now. Thank you for your post and your additional help! Time for me to purchase your books!
I’m very glad to hear you got it working!
You may need the new path to take effect.
Consider closing and opening the terminal window or restarting the computer.
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.
Consider a reboot?
scipy: 0.19.0
numpy: 1.12.1
matplotlib: 2.0.0
pandas: 0.19.2
statsmodels: 0.8.0
sklearn: 0.18.1
Very nice Ram!
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.
Ouch. You could try skipping statsmodels. It’s not needed for deep learning.
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
Nice work!
Sorry, I don’t know about readline, I have not seen this message before.
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
Very nice Pradep!
scipy: 0.19.1
numpy: 1.13.1
matplotlib: 2.0.2
pandas: 0.20.3
statsmodels: 0.8.0
sklearn: 0.18.2
Very nice Ishmael!
scipy: 0.19.1
numpy: 1.13.1
matplotlib: 2.0.2
pandas: 0.20.3
statsmodels: 0.8.0
sklearn: 0.19.0
theano: 0.9.0
tensorflow: 1.3.0
keras: 2.0.7
Thank you for the clear documentation.
Well done!
scipy: 1.0.0
numpy: 1.14.0
matplotlib: 2.1.1
pandas: 0.22.0
statsmodels: 0.8.0
sklearn: 0.19.1
Nice one!
scipy: 1.0.0
numpy: 1.14.0
matplotlib: 2.1.1
pandas: 0.22.0
statsmodels: 0.8.0
sklearn: 0.19.1
SORRY. TYPO ON EMAIL.
So 3.5 is the py version compatible all these deep learning libs? What about Anaconda? I had some trouble running sklearn from your missing values post using the Pima dataset. I checked ‘which python’ and I had custom version of 3.6 that included a weird hash in the filename?
Python 3.6 is fine too. I use it myself now.
theano: 1.0.1
tensorflow: 1.4.1
Using TensorFlow backend.
keras: 2.1.3
Well done!
altgraph==0.15
bleach==1.5.0
Bottleneck==1.2.1
cycler==0.10.0
Cython==0.27.3
enum34==1.1.6
html5lib==0.9999999
Keras==2.1.3
macholib==1.9
Markdown==2.6.11
matplotlib==2.1.1
modulegraph==0.16
nose==1.3.7
numexpr==2.6.4
numpy==1.14.0
pandas==0.22.0
patsy==0.5.0
protobuf==3.5.1
py2app==0.14
pycairo==1.15.4
pyobjc-core==3.0.4
pyobjc-framework-Cocoa==3.0.4
pyparsing==2.2.0
python-dateutil==2.6.1
pytz==2017.3
PyYAML==3.12
scikit-learn==0.19.1
scipy==1.0.0
six==1.11.0
statsmodels==0.8.0
tables==3.4.2
tensorflow==1.4.1
tensorflow-tensorboard==0.4.0
Theano==1.0.1
tornado==4.5.2
Werkzeug==0.14.1
The directory ‘/Users/briangriner/Library/Caches/pip/http’ or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo’s -H flag.
Hum… I wonder who owns that directory and who disabled the cache?
I ignore that warning.
scipy: 1.0.0
numpy: 1.14.0
matplotlib: 2.1.1
pandas: 0.22.0
statsmodels: 0.8.0
sklearn: 0.19.1
Well done!
Is there a reason to use python35 over python36?
Great post! Switched from homebrew to MacPorts after reading this. Couldn’t get a stable env with homebrew. Thank you!
No. I use Python 3.6 now. I will update the post soon.
scipy: 1.0.0
numpy: 1.14.0
matplotlib: 2.1.2
pandas: 0.22.0
statsmodels: 0.8.0
sklearn: 0.19.1
theano: 1.0.1
tensorflow: 1.5.0
Using TensorFlow backend.
keras: 2.1.4
Well done!
scipy: 1.0.0
numpy: 1.14.1
matplotlib: 2.1.1
pandas: 0.22.0
statsmodels: 0.8.0
sklearn: 0.19.1
Nice work!
while I use Command + B in Sublime Text 3, I have got the following versions for versions.py. It is pretty good.
scipy: 1.0.1
numpy: 1.14.2
matplotlib: 2.1.1
pandas: 0.22.0
sklearn: 0.19.1
[Finished in 1.2s]
Also, I test deep_versions.py fo r the following result. But TensorFlow seems has a problem in runtime.
theano: 1.0.1
/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/importlib/_bootstrap.py:222: RuntimeWarning: compiletime version 3.6 of module ‘tensorflow.python.framework.fast_tensor_util’ does not match runtime version 3.5
return f(*args, **kwds)
tensorflow: 1.7.0
Using TensorFlow backend.
keras: 2.1.5
[Finished in 2.5s]
Nice work!
Although I get the result of the deep_versions, there is the warning occured: RuntimeWarning: compiletime version 3.6 of module ‘tensorflow.python.framework.fast_tensor_util’ does not match runtime version 3.5
return f(*args, **kwds)
tensorflow: 1.7.0
Is this problem serious? How can solve the matching problem between compiletime version 3.6 and runtime version 3.5?
Please give me your advice. Thanks in advance.
No, safe to ignore. You could look at updating to Python 3.6.
scipy: 1.1.0
numpy: 1.15.4
matplotlib: 3.0.2
pandas: 0.23.4
statsmodels: 0.9.0
sklearn: 0.20.1
so far so good!
Well done!
theano: 1.0.3
tensorflow: 1.12.0
Using TensorFlow backend.
keras: 2.2.4
Very nice!
HI Jason, Do I really need Xcode? Seems unable to select te python interpreter. If so, I cannot use this IDE. Any suggestions?
Here is an alternative approach to setting up your environment:
https://machinelearningmastery.com/setup-python-environment-machine-learning-deep-learning-anaconda/
I recommend using a text editor instead of an IDE.
ok, tnx
scipy: 1.2.0
numpy: 1.15.4
matplotlib: 3.0.2
pandas: 0.23.4
statsmodels: 0.9.0
sklearn: 0.20.2
Well done!
What is macports needed for?
Hi I built a new environment following these intructions and rather happy to have a stable environment but remain unclear as to the purpose of macports
What does it achieve here?
Thank you
Macports is used to install libraries on mac, it is an alternative to brew, and comparable to yum/rpm on linux.
Hi Jason,
we don’t you use or propose virtualenv. Sooner or later we will have time where one experiment needs a different version of ML/DL libraries for compatibility than the other.
Perhaps.
i think i have a problem here
Antoninas-MacBook-Pro:~ antoninapanagiotou$ python versions.py
scipy: 1.3.0
numpy: 1.16.4
matplotlib: 3.1.1
Fontconfig warning: ignoring UTF-8: not a valid region tag
pandas: 0.25.0
statsmodels: 0.10.0
Traceback (most recent call last):
File “versions.py”, line 17, in
import sklearn
ModuleNotFoundError: No module named ‘sklearn’
It looks like scikit-learn is not installed.
something went wrong when I tried an example and here is the thing..
sudo pip install theano
Requirement already satisfied: theano in /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (1.0.4)
Requirement already satisfied: numpy>=1.9.1 in /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from theano) (1.16.4)
Requirement already satisfied: scipy>=0.14 in /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from theano) (1.3.0)
Requirement already satisfied: six>=1.9.0 in /opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from theano) (1.12.0)
(base) Antoninas-MacBook-Pro:~ antoninapanagiotou$ python deep_versions.py
Traceback (most recent call last):
File “deep_versions.py”, line 2, in
import theano
ModuleNotFoundError: No module named ‘theano’
Interesting. Perhaps you have two versions of Python installed, where pip is installing for one, and the python interpreter is running another?
no i’ve setted the default one python3.7
i don’t know if by installing anaconda.pkg everything messed up
Loving your tutorials.
scipy: 1.3.1
numpy: 1.17.1
matplotlib: 3.0.3
pandas: 0.25.1
statsmodels: 0.10.1
sklearn: 0.21.3
But I also received these messages:
Unable to revert mtime: /usr/X11/lib/X11/fonts/Speedo
Unable to revert mtime: /usr/X11/lib/X11/fonts/cyrillic
Unable to revert mtime: /usr/X11/lib/X11/fonts/util
Unable to revert mtime: /usr/X11/lib/X11/fonts/encodings/large
The deep learning script returned a lot of future warnings.
Nice work!
Ouch, perhaps ignore for now?
Yes, Keras needs an update, it is long over due! Ignore the warnings from TF for now.
scipy: 1.3.1
numpy: 1.17.2
matplotlib: 3.1.1
pandas: 0.25.1
statsmodels: 0.10.1
sklearn: 0.21.3
theano: 1.0.4
tensorflow: 2.0.0
Using TensorFlow backend.
keras: 2.3.1
Well done Andrew!
Dear Jason:
Thank you for the support and tutorials
Motivation: Due to recent mac os upgrade from Mojave to Catalina 10.15 they uninstall anaconda from root directories and because I used the old 2.7 anaconda I decided to reinstall all my python+tensorflow+keras environment according to this tutorials.
Here are my experience that I share according to your instructions:
1) after installing MacPorts …you have to close and reopen terminal in order to get the right answer of updated to the command : ‘sudo port selfupdate’
2) I though Anaconda or Miniconda take better care of modules dependencies installation, but the way you propose through MacPorts you have to take car of it…is it right?
3) to me installing Keras and tensorflow is the base for my software, but Keras said it required that the only current python compatibility is 2.7 or 3.6 version …so installing 3.5 python must be corrected from this tutorial to 3.6
4) in keras page installation to other software are recommended for installation:
4.1) h5py, that I understand it is already included in pyhton 3.6…nothing additional to do it
4.2) pydot (for visual python utilities)…but because they recommended install by pip … I decided to do the same command installation structure as you do for scikit-leanr with pip :1
sudo pip install -U pydot am I right?
4.3) regarding graphviz (also a visual utility) I follow the Macport recommedation installation:
sudo port install py36-graphviz … is it right?
But I see that they install dependencies like python 3.7 …so I am little concern if something is wrong …
so jason my questions regarding this item 4 are finally :
do we to install ‘pydot’ and ‘graphviz’?
and how? to avoid dependencies confusion…
5) I install theano, but I am not interested in used it…anyway how tensorflow ‘magically’ is running as backend under Keras, if the commands for tensorflow and theano installation are the same privileges?
6) i was using Atom for python script editions but It is not currently possible installing …due to Catalina…any suggestion of how to do it or which other sofware suggestion for replacing it?
7) I also share my installed versions
scipy: 1.4.1
numpy: 1.17.4
matplotlib: 3.1.2
pandas: 0.25.3
statsmodels: 0.10.2
sklearn: 0.22
h5py: 2.10.0
graphviz: 0.12
pydot: 1.4.1
theano: 1.0.4
tensorflow: 2.0.0
Using TensorFlow backend.
keras: 2.3.1
thank you for your nice job
JG
Nice work. Thanks for sharing.
From memory, I believe you install pydot and graphviz, not pygraphviz.
Dear Jason,
I also experience the :
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] on my Mac using Macport installation manager for my Python 3.6/keras …environment
I noticed it when I try to download any dataset from any Internet url …when apply for example keras dataset methods such as as “.load_data” for datasets as a MNIST, CIFAR, etc.
Following your specific recommendation, I apply the following Unix command to really install certifications verifications for any url Internet access:
$ sudo port install curl-ca-bundle
And the problem was solved !
Thank your very much Jason for your continuous help and support on these matters !
regards
JG
Well done!
Thanks for sharing.
In addition to add pydot and h5py (with pip installer) and graphviz (with macports installer) libraries for visualization and disk saving keras utilities
I have noticed that for images keras useful preprocessing library we have to add also Pillow library (I install also with pip using -U option )…
So I recommend to add these to this tutorials for Keras/tensorflow development…
so now I expect to have an standard library for keras machine learning
regards
Absolutely!
I will schedule time to update the tutorial.
I followed these instructions but when I type python -V I get, Python 2.6.15 but I should get Python3.7. I used sudo port select –set python python37 but it made no difference?
I am also getting the error “no module pandas found” when I run versions.py.
Any ideas? Thanks!
That is odd.
Ensure that macports was install for the whole syste,
Ensure the ports were instaleld using sudo.
Perhaps close and re-open your terminal window after installing and checking the version.
Let me know how you go.
I rebooted and everything works! The only remaining stumble is an error “no module ‘stats models’ found?
Thank you so much.
Well done!
It suggests statsmodels is not installed. You don’t need statsmodels unless you are using univariate time series models or some statistical methods.
Hi! I went through all of these steps and my computer took several hours installing of these directories like numpy and matplotlib, yet when I run version.py I am still getting the error of “No module named ‘numpy'”. I’ve been trying to figure out how to install these all day long. How can I get them to work? I am at a loss.
Sorry to hear that – this is an advanced tutorial, perhaps try this much simpler tutorial:
https://machinelearningmastery.com/setup-python-environment-machine-learning-deep-learning-anaconda/