These instructions detail how I install the scientific Python stack on my Mac. You can always check the Install Python page for other installation options.
I’m running the latest OS X Mountain Lion (10.8) but I think these instructions should work back to Snow Leopard (10.6). These instructions differ from my previous set primarily in that I now use Homebrew to install NumPy, SciPy, and matplotlib. I do this because Homebrew makes it easier to compile these with non-standard options that work around an issue with SciPy on OS X.
I’ll show how I install Python and the basic scientific Python stack:
If you need other libraries they can most likely be installed via pip and any dependencies can probably be installed via Homebrew.
Command Line Tools
The first order of business is to install the Apple command line tools. These include important things like development headers, gcc
, and git
. Head over to developer.apple.com/downloads, register for a free account, and download (then install) the latest “Command Line Tools for Xcode” for your version of OS X.
If you’ve already installed Xcode on Lion or Mountain Lion then you can install the command line tools from the preferences. If you’ve installed Xcode on Snow Leopard then you already have the command line tools.
Homebrew
Homebrew is my favorite package manager for OS X. It builds packages from source, intelligently re-uses libraries that are already part of OS X, and encourages best practices like installing Python packages with pip.
To install Homebrew paste the following in a terminal:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
The brew
command and any executables it installs will go in the directory /usr/bin/local
so you want to make sure that goes at the front of your system’s PATH
. As long as you’re at it, you can also add the directory where Python scripts get installed. Add the following line to your .profile
, .bash_profile
, or .bashrc
file:
export PATH=/usr/local/bin:/usr/local/share/python:$PATH
At this point you should close your terminal and open a new one so that this PATH
setting is in effect for the rest of the installation.
Python
Now you can use brew
to install Python:
brew install python
Afterwards you should be able to run the commands
which python
which pip
and see
/usr/local/bin/python
/usr/local/bin/pip
for each, respectively. (It’s also possible to install Python 3 using Homebrew: brew install python3
.)
NumPy
It is possible to use pip
to install NumPy, but I use a Homebrew recipe so I avoid some problems with SciPy. The recipe isn’t included in stock Homebrew though, it requires “tapping” two other sources of Homebrew formula:
brew tap homebrew/science
brew tap samueljohn/python
You can learn more about these at their respective repositories:
With those repos tapped you can almost install NumPy, but first you’ll have
to use pip
to install nose:
pip install nose
I compile NumPy against OpenBLAS to avoid a SciPy issue. Compiling OpenBLAS requires gfortran, which you can get via Homebrew:
brew install gfortran
brew install numpy --with-openblas
SciPy
And then you’re ready for SciPy:
brew install scipy --with-openblas
matplotlib
matplotlib generally installs just fine via pip
but the custom Homebrew formula takes care of installing optional dependencies too:
brew install matplotlib
IPython
You’ll want Notebook support with IPython and that requires some extra dependencies, including ZeroMQ via brew
:
brew install zeromq
pip install jinja2
pip install tornado
pip install pyzmq
pip install ipython
pandas
Pandas should install via pip
:
pip install pandas
Testing It Out
The most basic test you can do to make sure everything worked is open up an IPython session and type in the following:
import numpy
import scipy
import matplotlib
import pandas
If there are no errors then you’re ready to get started! Congratulations and enjoy!
These instructions are straightforward and quite helpful. They’re also a top Google result for searches on installing Python using Homebrew, so I’ll post a problem I had and how I resolved it, in case anyone following these instructions has a similar problem.
Ipython wouldn’t run, failing with the message:
#!/usr/bin/python
# EASY-INSTALL-ENTRY-SCRIPT: ‘ipython==0.13.dev’,’console_scripts’,’ipython’
__requires__ = ‘ipython==0.13.dev’
import sys
from pkg_resources import load_entry_point
sys.exit(
load_entry_point(‘ipython==0.13.dev’, ‘console_scripts’, ‘ipython’)()
)
”
More searching led to a comment here: “https://github.com/ipython/ipython/issues/814#issuecomment-14153416”
At the terminal “which python” returned “/usr/local/bin/python” as it should, but that script was requiring ipython==0.13.dev, . But “pip show ipython” indicates it installs version 0.13.2. Hm.
Finally, actually reading the output message from my last attempt to install ipython, I saw pip told me “Installing ipython script to /usr/local/share/python”, and that script also calls for version 0.13.2.
I copied the ipython script from /usr/local/share/python to /usr/local/bin/python, and now it works fine.
In the error message IPython is pointed at /usr/bin/python, which would be the wrong Python after installing with Homebrew. Is it possible you had a pre-existing install of IPython that was conflicting?
After poking around some more following my previous long-winded comment, I also noticed that my path had /usr/local/bin/ before /usr/local/bin/share/python, which caused it to find what must have been an ipython script from an old install first. Switching the path placement and deleting the script both solve the issue, too.
Thank-you for the guide. How can it be specified that only 32-bit Python/Numpy/Scipy/etc. are to be installed with Homebrew?
Also, has the Scipy/OSX problem been fixed with Scipy 0.12.1 ?
I’m not sure it’s possible to compile Python in 32-bit using Homebrew. If you need something that specific you might need to do the compilation yourself. I haven’t poked around with SciPy recently, so not sure what the status is.
in attempting to install Pandas, I was met with this:
error: /usr/local/lib/python2.7/site-packages/dateutil/easter.pyc: Permission denied
so when I did the IPython test, everything ran fine except import pandas.
Do you know how I can fix this? I’m admittedly not very savvy when it comes to things like the terminal, so your easy to use instructions here have really been helping.
No idea why you’d have a problem with dateutil and nothing else. Try again? You can uninstall with
pip uninstall dateutil
andpip uninstall pandas
and then try installing pandas again.[…] used these installation instructions here: https://penandpants.com/2013/04/04/install-scientific-python-on-mac-os-x/ without many […]
[…] https://penandpants.com/2013/04/04/install-scientific-python-on-mac-os-x/ has a good write up on how to install most of these modules using Homebrew and pip. […]
Hello,
Thanks for these installation instructions. When I am executing
brew install numpy –with-openblas
I get the error message:
Error: Operation already in progress for openblas
Another active Homebrew process is already using openblas.
Please wait for it to finish or terminate it to continue.
I got this error message the very first time I ran that command.
Any idea how to troubleshoot this?
Thanks
No idea what’s going on there. If the problem persists you might report an issue on the homebrew-python repo at https://github.com/Homebrew/homebrew-python.
I had the same problem and found the solution here (https://github.com/Homebrew/homebrew-science/pull/767). Basically, `brew edit numpy` and change the one place it has `homebrew/science/openblas` to `openblas’. Hope this helps!
I encountered the same problem and found a fix in the homebrew-science github forums (Homebrew/homebrew-science#767). Basically, edit the formula for numpy (”brew edit numpy”) and replace the reference to `homebrew/science/openblas` with `openblas`. Hope this works for you too!
Guillaume, read: https://github.com/xianyi/OpenBLAS/issues/331
to solve your problem, ‘brew edit openblas’ and in the openblas string substitute “0.2.9.rc2” for “0.2.8”.
then:
Archive: /Library/Caches/Homebrew/openblas-2.tar.gz
To retry an incomplete download, remove the file above.
just below the openblas line containing “0.2.8” you’ll also need to change the expected SHA1 hash to match the new file:
Expected: d012ebc2b8dcd3e95f667dff08318a81479a47c3
Actual: 60fbd30e94bb37e510fddd46bcfadfdb44cfff8c
YMMV
jy
On 10.9.4 I get:
“`
$ brew install gfortran
Error: No available formula for gfortran
GNU Fortran is now provided as part of GCC, and can be installed with:
brew install gcc
“`
Hello,
Sorry for my ignorance. I’m basically a Windows guy, trying to install IPython and Pandas on Mac OS X Yosemite Version 10.10.3.
After installing Homebrew successfully ran ‘brew install python’. It showed that it installed 2.7.9 but my python version is still showing 2.7.6. As a result I cannot continue installing IPython and Pandas. When I run the command ‘brew install python’ again, it shows the following:
‘Warning: python-2.7.9 already installed, it’s just not linked’.
I understand, the link has not been created in the proper path but I do not know where 2.7.9 has been installed.
Any help is greatly appreciated.
Rafiq
I recommend installing scientific Python using Anaconda unless you have a good reason not to. https://store.continuum.io/cshop/anaconda/
Update: Install Homebrew with;
ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
This is not working for my Mac Sierra version 10.12.5. The reply is “touch:…. permission denied”
These instructions are now pretty old. I’d recommend installing Anaconda unless you have a compelling reason to use something else.
Holy Lick! I was having a lot of issues trying to use the modules I had installed, thought what the hell I’m gonna try out this guide, and in the process I realized what happened and what I needed to do (I was missing a sys link between versions) fixed it up and I’m back in business. Thank you for this guide!! Cheers!!