Update: These instructions are over a year old, though they may still work for you. See the “Install Python” page for the most recent instructions.
A bit ago a friend and I both had fresh Mac OS X Lion installs so I helped him set up his computers with a scientific Python setup and did mine at the same time.
These instructions are for Lion but should work on Snow Leopard or Mountain Lion without much trouble. On Snow Leopard you won’t install Xcode via the App Store, you’ll have to download it from Apple.
After I’d helped my friend I found this blog post describing a procedure pretty much the same as below.
Update: If doing all the stuff below doesn’t seem like your cup of tea, it’s also possible to install Python, NumPy, SciPy, and matplotlib using double-click binary installers (resulting in a much less flexible installation), see this post to learn how.
Xcode
You will need Apple’s developer tools in order to compile Python and the other installs. On Lion you can install Xcode from the App Store, on Snow Leopard you’ll have to get an older Xcode from developer.apple.com.
I use the Xcode editor because I like its syntax highlighting, code completion, and organizer. However, I use hardly any of its features and unless you’re an iOS or Mac developer you probably won’t either. If you prefer another editor it’s possible to get only the libraries and compilers that you need with the Command Line Tools for Xcode. (You’ll need a free Apple ID.) (See also http://www.kennethreitz.com/xcode-gcc-and-homebrew.html.)
Homebrew
Homebrew is an excellent package manager for Mac OS X that can install a large number of packages. To install it simply launch a terminal and enter
ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
Homebrew installs things to /usr/local/
so you don’t need sudo
permissions. To add Homebrew installed executables and Python scripts to your path you’ll want to add the following line to your .profile
(or .bash_profile
) file:
export PATH=/usr/local/bin:/usr/local/share/python:$PATH
Normal executables go in /usr/local/bin/
and Python scripts installed by Homebrew go in /usr/local/share/python/
.
See https://github.com/mxcl/homebrew/wiki/The-brew-command or type brew help
or man brew
for more info on Homebrew.
Install Python
Now that you’ve got Homebrew installing Python is simple:
brew install python
Homebrew will install a couple of packages required by Python and then Python itself. Don’t be surprised if this takes a couple minutes.
Important: You should close your terminal and open a fresh one right now so that it has the updated PATH from the previous section. Otherwise you run the risk of executing the wrong scripts during the rest of these instructions.
At this point you should be able to get a fresh terminal and type
which python
and see
/usr/local/bin/python
Homebrew is for installing system packages and tools; for managing Python add-ons we want pip. Luckily easy_install, another Python package manager is installed by Homebrew and we can use it to install pip:
easy_install pip
Install NumPy
Use pip to install NumPy:
pip install numpy
This should install NumPy 1.6.1 (as of Feb. 2012).
Install SciPy
We need gfortran to compile SciPy but it is not included with the other Xcode tools. Luckily, Homebrew can help us out again:
brew install gfortran
When that’s done it’s a cinch to install SciPy:
pip install scipy
This should install SciPy 0.10.
Install matplotlib
To install matplotlib we need to revisit Homebrew one more time:
brew install pkg-config
And the usual pip command:
pip install matplotlib
This should install matplotlib 1.2.0. If it doesn’t you can try installing from the matplotlib development repo:
pip install git+git://github.com/matplotlib/matplotlib.git
Congratulations! You should now have the basics of a scientific Python installation that’s easy to manage and upgrade using Homebrew and pip. Fire up Python and make sure things worked. The following should work in Python with no errors:
import numpy import scipy import matplotlib
Enjoy!
On latest lion with command line compiler tools installed, matplotlib installation fails:
agg24/include/agg_renderer_outline_aa.h:1368:45: error: binding of reference to type ‘agg::line_profile_aa’ to a value of type ‘const agg::line_profile_aa’ drops qualifiers
line_profile_aa& profile() { return *m_profile; }
^~~~~~~~~~
1 error generated.
error: command ‘/usr/bin/clang’ failed with exit status 1
I tried replacing clang with llvm-gcc-4.2 (just overwrite temporarily clang) — same thing.
Any ideas?
Which command did you use to install matplotlib, and did you notice what version it’s trying to install?
I tried both:
pip install matplotlib
that tried to install:
matplotlib: 1.1.0
python: 2.7.2 (default, Feb 25 2012, 09:03:36) [GCC 4.2.1
Compatible Apple Clang 3.1
(tags/Apple/clang-318.0.45)]
platform: darwin
and
pip install git+git://github.com/matplotlib/matplotlib.git
no difference in result.
It looks like this is a known bug in matplotlib that should be fixed soon. In fact, it looks like it’s already been fixed in the development version. You might try reinstalling from github. You might also want to take a look at this thread from a few days ago.
I’m not sure why your matplotlib is building with clang. I just did
pip install matplotlib
on my machine and it compiles withllvm-gcc
.(looks like deeper replies are not allowed)
The latest github state passes: Yay – thank you!
It’d be better to write this as a single Ruby script and post the one command that any Mac user can run to install everything from scratch.
Please do!
Hi Jiffyclub,
I have a problem running homebrew command in my mac terminal. I get the following error:
ruby -e “$(curl -fsSkL raw.github.com/mxcl/homebrew/go)”
File “”, line 1
ruby -e “$(curl -fsSkL raw.github.com/mxcl/homebrew/go)”
^
SyntaxError: invalid syntax
Please help if you can
Thanks!
Hi Sylvester, which shell are you using? I seem to remember that the Homebrew install command is specific to bash.
Matt – thanks for writing this. I’d been meaning to get started with Python for months, and this easy tutorial gave me motivation to do it.
Couple things:
(1) I had trouble with the curl command at the very beginning for homebrew; with help from an officemate, we fixed that by copying the script to a .rb file and running it with /usr/bin/ruby.
(2) Everything appeared to install fine, and I did a rehash, but after starting python and trying to import the new packages I get:
ImportError: No module named numpy
Thoughts on that?
One thing is to make sure you’ve started the right Python. If you type
which python
in a terminal do you get/usr/local/bin/python
? If instead you get/usr/bin/python
then you want to set yourPATH
as described in the Homebrew section above.If you’ve got the right Python but still no NumPy check this directory, that’s where it should be installed:
/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/
. If you don’t see it there then it was probably installed somewhere else for some reason. You’ll again want to check yourPATH
and make sure you’re using the right easy_install and pip.which pip
should give/usr/local/share/python/pip
. If you get a different pip you’ll again want to make sure you’ve got yourPATH
set correctly and start again from theeasy_install pip
step.I installed numpy but from there onwards everything is bad! When I do which pip I get “/usr/local/bin/pip”. When I go into /usr/local/Cellar/python/2.7.3 there isn’t even a lib file! Any ideas? This is my .bash_profile:
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH=”/usr/local/bin/python:${PATH}”
export PATH=/usr/local/bin:/usr/local/share/python:$PATH
Sorry to be a hassle! I really need to get this working asap. Thanks, Nick
Except for the fact that you don’t need to put
/usr/local/bin/python
on yourPATH
this all looks fine. What exact errors are you seeing?I have the same problem as Nick,
pip is installed in /usr/local/bin/pip, but links to pip inside the Cellar, so that seems OK.
problems start when installing scipy, starting with:
umfpack_info:
libraries umfpack not found in /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib
libraries umfpack not found in /usr/local/lib
libraries umfpack not found in /usr/lib
/usr/local/lib/python2.7/site-packages/numpy/distutils/system_info.py:470: UserWarning:
UMFPACK sparse solver (http://www.cise.ufl.edu/research/sparse/umfpack/)
not found. Directories to search for the libraries can be specified in the
numpy/distutils/site.cfg file (section [umfpack]) or by setting
the UMFPACK environment variable.
warnings.warn(self.notfounderror.__doc__)
NOT AVAILABLE
and much more packages that can’t be found after that
The /usr/local/Cellar/python/2.7.3/Frameworks/Python.framwork/Versions/2.7/lib folder doesn’t exist, as nick stated. Has something changed in the current version? I don’t get it…
Ahhh sorry for not doing enough googling:
fixed it by manually compiling numpy and then installing with:
python setup.py install –prefix=/usr/local
cheers,
Lennard
Glad you got it working! Homebrew installs pip to
/usr/local/share/python
so it’s odd you have one in/usr/local/bin
. That may be part of the issue and manually installing NumPy works around it.as far as I know numpy now installs with the python from homebrew, I made sure I deleted all other versions of python. Also /usr/local/bin/python links to the Cellar. In /usr/local/lib/python2.7/ there are libraries needed for compiling scipy for instance.
Although scipy now compiles, scipy.test() results in 50+ errors. Apparently it’s a known bug, something to do with gfortran and the like with macosx, didn’t find anything on how to solve it yet.
If you run an matplotlib example, you may find the script though not showing any errors, just hanges and does not plotting anything.
Edit ~/.matplotlib/matplotlibrc and add:
backend: MacOSX
Worked for me.
[…] already written a post about installing Python, NumPy, SciPy, and matplotlib on Lion, but it involves a lot of working at the command line, modifying your .bash_profile and […]
Thanks for the post, it motivated me to finally get all this installed on my mac. It seems the url for installing homebrew has changed, though. https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb worked for me (March 24).
Thanks for letting me know!
btw pointed a few ppl your way – http://www.astrobetter.com/learning-python-the-interactive-way/
Thanks for the great post.
It could be nice to advice to run `brew doctor` and remove old dependencies for systems that are not clean. Otherwise installing `pkg-config` and `gfortran` can be useless (because shadowed by old libraries).
Hey Matt!
I really appreciate the instructions you’ve been posting on here. It was super-convenient to use the double-click install on my work machine, however I wish that python had tab completion (presumably from readline). Does this “CIY” version compile with readline, or support tab completion some other way?
Thanks!
If you want tab completion you’ll want IPython: http://ipython.org/. The IPython HTML Notebook is highly recommended.
You should be able to install IPython regardless of which method you used to install Python, etc.
That’s awesome you’re using Python at work! Good to hear from you!
Yeah, after seeing the HTML Notebook on your site that’s my goal–I have some class exercises in IDL I’d like to rewrite in python. So you totally answered my question, but I asked something different that what I intended. =) I’m actually looking for the unix-like command line editing that I believe readline provides. I’m wondering if that just works with the CIY method.
I also have just learned that, apparently, the libedit library on OS X can provide this, and it may be possible to turn that on post-install with just a configuration file.
Thanks, I appreciate you sharing your expertise! The last time I installed a computer with all my analysis software was more than a few years ago, and a lot has changed since then!
Homebrew does compile Python against readline so all of the Control-letter commands work in the interactive interpreter. However, I’d recommend the IPython notebook regardless.
I had trouble with scipy, this was necessary: pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev
This article save my life. Thank you so much.
Glad to help!
Hi Matt,
This is awesome. Thanks so much. These were the changes to your directions above that I had to make, all very painless:
Homebrew url changed again. I used: /usr/bin/ruby -e “$(/usr/bin/curl -fsSkL raw.github.com/mxcl/homebrew/go)”
I didn’t have to do: brew install pkg-config, apparently pkg-config was already installed.
I’m adding some comments to your Mountain Lion Post as well, I ran into more problems than you did.
-Azalee
I follow your post to install python and numpy.
I successfully installed python on my mac book (Mac OS X Lion 10.7.5) but after running the command pip install numpy I get this error:
Downloading/unpacking numpy
Running setup.py egg_info for package numpy
Running from numpy source directory.non-existing path in ‘/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils’: ‘site.cfg’
F2PY Version 2
blas_opt_info:
FOUND:
extra_link_args = [‘-Wl,-framework’, ‘-Wl,Accelerate’]
define_macros = [(‘NO_ATLAS_INFO’, 3)]
extra_compile_args = [‘-msse3’, ‘-I/System/Library/Frameworks/vecLib.framework/Headers’]
lapack_opt_info:
FOUND:
extra_link_args = [‘-Wl,-framework’, ‘-Wl,Accelerate’]
define_macros = [(‘NO_ATLAS_INFO’, 3)]
extra_compile_args = [‘-msse3’]
build_src
building py_modules sources
building library “npymath” sources
customize NAGFCompiler
Could not locate executable f95
customize AbsoftFCompiler
Could not locate executable f90
Could not locate executable f77
customize IBMFCompiler
Could not locate executable xlf90
Could not locate executable xlf
customize IntelFCompiler
Could not locate executable ifort
Could not locate executable ifc
customize GnuFCompiler
Could not locate executable g77
customize Gnu95FCompiler
Could not locate executable gfortran
customize G95FCompiler
Could not locate executable g95
customize PGroupFCompiler
Could not locate executable pgfortran
don’t know how to compile Fortran code on platform ‘posix’
C compiler: xcrun clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
compile options: ‘-I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/private -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/npymath -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/multiarray -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/umath -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/include -I/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c’
xcrun: _configtest.c
xcrun clang _configtest.o -o _configtest
ld: library not found for -lcrt1.10.6.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lcrt1.10.6.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
failure.
removing: _configtest.c _configtest.o
Traceback (most recent call last):
File “”, line 16, in
File “/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/setup.py”, line 214, in
setup_package()
File “/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/setup.py”, line 207, in setup_package
configuration=configuration )
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/core.py”, line 186, in setup
return old_setup(**new_attr)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py”, line 152, in setup
dist.run_commands()
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py”, line 953, in run_commands
self.run_command(cmd)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py”, line 972, in run_command
cmd_obj.run()
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/egg_info.py”, line 8, in run
self.run_command(“build_src”)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py”, line 326, in run_command
self.distribution.run_command(command)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py”, line 972, in run_command
cmd_obj.run()
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 152, in run
self.build_sources()
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 163, in build_sources
self.build_library_sources(*libname_info)
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 298, in build_library_sources
sources = self.generate_sources(sources, (lib_name, build_info))
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 385, in generate_sources
source = func(extension, build_dir)
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/setup.py”, line 696, in get_mathlib_info
raise RuntimeError(“Broken toolchain: cannot link a simple C program”)
RuntimeError: Broken toolchain: cannot link a simple C program
Complete output from command python setup.py egg_info:
Running from numpy source directory.non-existing path in ‘/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils’: ‘site.cfg’
F2PY Version 2
blas_opt_info:
FOUND:
extra_link_args = [‘-Wl,-framework’, ‘-Wl,Accelerate’]
define_macros = [(‘NO_ATLAS_INFO’, 3)]
extra_compile_args = [‘-msse3’, ‘-I/System/Library/Frameworks/vecLib.framework/Headers’]
lapack_opt_info:
FOUND:
extra_link_args = [‘-Wl,-framework’, ‘-Wl,Accelerate’]
define_macros = [(‘NO_ATLAS_INFO’, 3)]
extra_compile_args = [‘-msse3’]
running egg_info
running build_src
build_src
building py_modules sources
building library “npymath” sources
customize NAGFCompiler
Could not locate executable f95
customize AbsoftFCompiler
Could not locate executable f90
Could not locate executable f77
customize IBMFCompiler
Could not locate executable xlf90
Could not locate executable xlf
customize IntelFCompiler
Could not locate executable ifort
Could not locate executable ifc
customize GnuFCompiler
Could not locate executable g77
customize Gnu95FCompiler
Could not locate executable gfortran
customize G95FCompiler
Could not locate executable g95
customize PGroupFCompiler
Could not locate executable pgfortran
don’t know how to compile Fortran code on platform ‘posix’
C compiler: xcrun clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -DNDEBUG -g -O3 -Wall -Wstrict-prototypes
compile options: ‘-I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/private -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/npymath -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/multiarray -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/src/umath -I/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/include -I/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c’
xcrun: _configtest.c
xcrun clang _configtest.o -o _configtest
ld: library not found for -lcrt1.10.6.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ld: library not found for -lcrt1.10.6.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
failure.
removing: _configtest.c _configtest.o
Traceback (most recent call last):
File “”, line 16, in
File “/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/setup.py”, line 214, in
setup_package()
File “/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/setup.py”, line 207, in setup_package
configuration=configuration )
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/core.py”, line 186, in setup
return old_setup(**new_attr)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/core.py”, line 152, in setup
dist.run_commands()
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py”, line 953, in run_commands
self.run_command(cmd)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py”, line 972, in run_command
cmd_obj.run()
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/egg_info.py”, line 8, in run
self.run_command(“build_src”)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/cmd.py”, line 326, in run_command
self.distribution.run_command(command)
File “/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py”, line 972, in run_command
cmd_obj.run()
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 152, in run
self.build_sources()
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 163, in build_sources
self.build_library_sources(*libname_info)
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 298, in build_library_sources
sources = self.generate_sources(sources, (lib_name, build_info))
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/distutils/command/build_src.py”, line 385, in generate_sources
source = func(extension, build_dir)
File “/private/var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy/numpy/core/setup.py”, line 696, in get_mathlib_info
raise RuntimeError(“Broken toolchain: cannot link a simple C program”)
RuntimeError: Broken toolchain: cannot link a simple C program
—————————————-
Command python setup.py egg_info failed with error code 1 in /var/folders/k9/3cg3j23s0slbwbym9fkshj9r0000gn/T/pip-build/numpy
Can you please help me with this.
Thanks
A commenter on another of my posts recommends this answer on StackOverflow: http://stackoverflow.com/a/14131249/1405710
thanks totally worked for me.
Thanks a bunch for this concise and correct guide to getting Python up and running on Mac. Was having a ‘mare with a clean install and following this sorted it right out!
Hi all … I have some tradition in building numpy/scipy (with optimized libs like suite-sparse) on the Mac (and Linux earlier). When I learned about Homebrew, I started to join the project to improve Python support and make the “scipy-superpack” kind of obsolete.
We know `pip` is bad at pulling dependencies. Therefore, I’d like to introduce my own Homebrew “tap” (=easy-to-add repo of additional formulae) at https://github.com/samueljohn/homebrew-python.
I can support you there (just open an issue) when you get into trouble (instead of spamming in this comments thread :-)
You can then:
`brew tap samueljohn/python`
and `brew install numpy scipy matplotlib pillow` (may tell you that you need to `pip install nose` because homebrew does not aim to include pure python modules that build just fine with pip). If you love bleeding edge, you may want to try `brew install numpy –HEAD`.
I’d love to get your feedback and suggestions. This is my little contribution to the Python community. Hope my homebrew scripts may save you the one or the other minute in getting a decent scientific Python setup on the Mac.
Looks neat, thanks for sharing!
hello,
thanks for the guide but i have prolem installing matplotlib.
i followed your instructions but got stocked at “pip install matplotlib”..here is the error message.
using the first option(pip install matplotlib), i get:
pymods [‘pylab’]
packages [‘matplotlib’, ‘matplotlib.backends’, ‘matplotlib.backends.qt4_editor’, ‘matplotlib.projections’, ‘matplotlib.testing’, ‘matplotlib.testing.jpl_units’, ‘matplotlib.tests’, ‘mpl_toolkits’, ‘mpl_toolkits.mplot3d’, ‘mpl_toolkits.axes_grid’, ‘mpl_toolkits.axes_grid1’, ‘mpl_toolkits.axisartist’, ‘matplotlib.sphinxext’, ‘matplotlib.tri’, ‘matplotlib.delaunay’, ‘pytz’, ‘dateutil’, ‘dateutil.zoneinfo’]
warning: no files found matching ‘KNOWN_BUGS’
warning: no files found matching ‘INTERACTIVE’
warning: no files found matching ‘make.osx’
warning: no files found matching ‘MANIFEST’
warning: no files found matching ‘__init__.py’
warning: no files found matching ‘examples/data/*’
warning: no files found matching ‘lib/mpl_toolkits’
warning: no files found matching ‘lib/matplotlib/mpl-data/matplotlib.conf’
warning: no files found matching ‘lib/matplotlib/mpl-data/matplotlib.conf.template’
warning: manifest_maker: MANIFEST.in, line 16: ‘recursive-include’ expects …
warning: no files found matching ‘LICENSE*’ under directory ‘license’
Installing collected packages: matplotlib
Running setup.py install for matplotlib
basedirlist is: [‘/usr/local/’, ‘/usr’, ‘/usr/X11’, ‘/opt/local’]
second options(downloading for the website), i get:
freetype2: found, but unknown version (no pkg-config)
cc -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -DNDEBUG -g -fwrapv -O3 -Wall -DPY_ARRAY_UNIQUE_SYMBOL=MPL_ARRAY_API -DPYCXX_ISO_CPP_LIB=1 -I/usr/local/include -I/usr/include -I/usr/X11/include -I/opt/local/include -I/usr/local/include -I/usr/include -I. -I/usr/local/lib/python2.7/site-packages/numpy/core/include -I. -I/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/_png.cpp -o build/temp.macosx-10.8-x86_64-2.7/src/_png.o
src/_png.cpp:23:13: fatal error: ‘png.h’ file not found
# include
^
1 error generated.
error: command ‘cc’ failed with exit status 1
—————————————-
Command /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -c “import setuptools;__file__=’/private/var/folders/7q/0s9d01f50gn_j01xd2_709jh0000gn/T/pip-build-Eberechi/matplotlib/setup.py’;exec(compile(open(__file__).read().replace(‘\r\n’, ‘\n’), __file__, ‘exec’))” install –record /var/folders/7q/0s9d01f50gn_j01xd2_709jh0000gn/T/pip-C70ej3-record/install-record.txt –single-version-externally-managed failed with error code 1 in /private/var/folders/7q/0s9d01f50gn_j01xd2_709jh0000gn/T/pip-build-Eberechi/matplotlib
Storing complete log in /Users/Eberechi/.pip/pip.log
thank you,hope you can help me. thank you
regards
I’d stick with “pip install matplotlib”, I just tried it and it worked for me installing version 1.2.1. I got very similar output to you but at the end it said “Successfully installed matplotlib”.
Thank you for the feed back. Is there a particular version of Anaconda that you would recommend for osx 10.6.8?
The archive of installers is at http://repo.continuum.io/archive/index.html. Maybe try 1.9.2. It looks like you’ll have to use the bash installer (the .sh file), not the graphical installer.
[…] 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 […]
[…] I found this to work flawlessly on OS X 10.8.3 https://penandpants.com/2012/02/24/install-python/ […]
You may want to add a missing piece:
brew install swig
SciPy installation would not proceed without it.
How were you installing SciPy? I’ve never seen that before and I’m pretty sure swig is not necessary to install SciPy.
FYI: I got this error installing matplotlib:
fatal error: ‘ft2build.h’ file not found
Fixed it by installing freetype and libpng via homebrew.
Reblogged this on Shanshan Chen and commented:
Great summary of python, numpy,scipy installation on a mac
[…] Add on and summarized from blog https://penandpants.com/2012/02/24/install-python/ […]
Thanks for the thread. I followed the steps exactly however when I type which python it comes out: usr/bin/python instead of the one brew does. How to change it?
First few lines in my .bash_profile:
PATH=”/usr/local/bin:$PATH”
export PATH
export PATH=/usr/local/bin:$PATH
export PATH=/usr/local/share/python:$PATH
test -r /sw/bin/init.sh && . /sw/bin/init.sh
—————————
Because I firstly ignore it and did the follow-ups, then I can’t even install pip…
error: can’t create or remove files in install directory
The following error occurred while trying to add or remove files in the
installation directory:
[Errno 13] Permission denied: ‘/Library/Python/2.7/site-packages/test-easy-install-21675.write-test’
The installation directory you specified (via –install-dir, –prefix, or
the distutils default setting) was:
/Library/Python/2.7/site-packages/
—————————
Then I went back to usr/local/bin I saw brew, ipython, gfortran, pkg-configure etc just everything should show up except python! Could you give me some suggestions?
I’m very much appreciated!
The
test -r /sw/bin/init.sh && . /sw/bin/init.sh
line in your .bash_profile suggests you have fink installed, which can break Homebrew. You might consider cleaning up your .bash_profile to remove stuff you no longer use.Have you thought about using Anaconda? See https://penandpants.com/install-python/.
Yes I did install fink before. But I’m not sure how to revise the bash profile without causing problems in my other programs… What I did was just following texts online… Dunno which line is redundant which is not….
My suspicion is that you are having trouble because the Homebrew installed stuff is not coming up first on your PATH. Fink is for sure messing with your PATH after you’ve added the Homebrew dirs. What do you see with
type pip
?Typing pip gives me the line:
There’s no pip command….
Fantastic post – at last I’ve resolved my scipy installation woes (I’ve just noticed your updated OSX installation page too).
Hi I found this article really useful (god what a pain scipy is to install on the mac). BUT it didn’t quite work for OSX 10.8.5. If, like me, you get the error:
KeyError: ‘FARCHFLAGS’
when trying to install scipy then you need to add the line :
export FARCHFLAGS=”-arch x86_64″
before you run
pip install scipy
Hope this helps
Thank you! Easiest implementation guide to SciPy I have found online!
I had to run `brew install freetype` in order to get matplotlib to install on 10.8.5 OSX
Nice instructions. Worked for me on Mavericks (10.9 OS X). I did, however, have to brew install freetype to get rid of the fatal error: ‘png.h’ file not found.
[…] a) You will need Numpy, Scipy installed first, because the tools is largely python. Simply way is to use ‘brew‘. For example, follow the instructions here. […]
[…] Installing Python, virtualenv, NumPy, SciPy, matplotlib and IPython on Lion or Mountain Lion 2. Install Python, NumPy, SciPy, and matplotlib on Mac OS X 3. MacOS X 命令查看CPU信息 这里主要是看cpu几核,几线程,主频多大之类的 > […]
I am new to this whole python thing.
Trying to follow your instructions to install everything on Mac OS X 10.6.8. I have followed all instructions up to installing NumPy. When I “which python” I get “/usr/local/bin/python”, when i “which pip” I get “/usr/local/bin/pip”, I dont know if this is a problem.
I am now trying to install NumPy, using “pip install numpy” I get
“Requirement already satisfied (use –upgrade to upgrade): numpy in /usr/local/lib/python2.7/site-packages/numpy-1.8.1-py2.7-macosx-10.6-x86_64.egg
Cleaning up…”
but when I try to import numpy in python (through the terminal) i get
“Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named numpy”
Can you help me out, if possible? Cheers
I would strongly encourage using the Anaconda Python distribution: https://store.continuum.io/cshop/anaconda/ It will save a lot of these headaches. In fact, these days I use Anaconda for all my personal development.
Does this only work on 10.7 and higher?
The current version of Anaconda is 10.7+, but you can download older versions of the installer. I don’t know which one will work on 10.6.
Thanks for all the help. I run 10.6.8, but I just downloaded from the link on the site and everything seems to work out fine.
I am trying to install python and I have been able to do everything but scipy. I get the following:
pip install scipy
Downloading/unpacking scipy
Cleaning up…
Exception:
Traceback (most recent call last):
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py”, line 122, in main
status = self.run(options, args)
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py”, line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py”, line 1197, in prepare_files
do_download,
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py”, line 1375, in unpack_url
self.session,
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/download.py”, line 546, in unpack_http_url
resp = session.get(target_url, stream=True)
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/_vendor/requests/sessions.py”, line 468, in get
return self.request(‘GET’, url, **kwargs)
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/download.py”, line 237, in request
return super(PipSession, self).request(method, url, *args, **kwargs)
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/_vendor/requests/sessions.py”, line 456, in request
resp = self.send(prep, **send_kwargs)
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/_vendor/requests/sessions.py”, line 559, in send
r = adapter.send(request, **kwargs)
File “/usr/local/Cellar/python/2.7.7_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg/pip/_vendor/requests/adapters.py”, line 384, in send
raise Timeout(e, request=request)
Timeout: HTTPSConnectionPool(host=’pypi.python.org’, port=443): Read timed out.
Storing debug log for failure in /Users/shaune/.pip/pip.log
HELP!!!!!!
I strongly recommend using the Anaconda Python distribution to get scientific Python: https://store.continuum.io/cshop/anaconda/.
[…] pandas, etc. on python 2.7 on my Mac (Mavericks 10.9.4) using pip install. I follow this page : https://penandpants.com/2012/02/24/install-python/ which works for me on a previous […]
Running into errors while trying to install scipy:
Obtaining scipy from git+https://github.com/scipy/scipy#egg=scipy-dev
Exception:
Traceback (most recent call last):
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/basecommand.py”, line 232, in main
status = self.run(options, args)
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/commands/install.py”, line 339, in run
requirement_set.prepare_files(finder)
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/req/req_set.py”, line 270, in prepare_files
logger.info(‘Obtaining %s’, req_to_install)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1021, in info
self._log(INFO, msg, args, **kwargs)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1129, in _log
self.handle(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1139, in handle
self.callHandlers(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1176, in callHandlers
hdlr.handle(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 658, in handle
rv = self.filter(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 558, in filter
for f in self.filters:
AttributeError: BetterRotatingFileHandler instance has no attribute ‘filters’
Traceback (most recent call last):
File “/usr/local/bin/pip”, line 8, in
load_entry_point(‘pip==6.0.8’, ‘console_scripts’, ‘pip’)()
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/__init__.py”, line 217, in main
return command.main(cmd_args)
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/basecommand.py”, line 258, in main
logger.critical(‘Exception:\n%s’, format_exc())
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1065, in critical
self._log(CRITICAL, msg, args, **kwargs)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1129, in _log
self.handle(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1139, in handle
self.callHandlers(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 1176, in callHandlers
hdlr.handle(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 658, in handle
rv = self.filter(record)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py”, line 558, in filter
for f in self.filters:
This appears to be a bug in Python 2.6.1, you need to use a different version of Python. I strongly suggest installing Anaconda unless for some reason you need the bleeding edge version of SciPy.
you just save my day, thanks!
For me, it is still complaining about 2.6. I tried to install Anaconda as well (python 3.4 https://www.continuum.io/downloads). How can I fix this? Please help
$python –version
Python 2.7.10
$pip install –upgrade pip
You are using pip version 6.0.8, however version 7.1.2 is available.
You should consider upgrading via the ‘pip install –upgrade pip’ command.
Collecting pip from https://pypi.python.org/packages/py2.py3/p/pip/pip-7.1.2-py2.py3-none-any.whl#md5=5ff9fec0be479e4e36df467556deed4d
Using cached pip-7.1.2-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 6.0.8
Uninstalling pip-6.0.8:
Exception:
Traceback (most recent call last):
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/basecommand.py”, line 232, in main
status = self.run(options, args)
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/commands/install.py”, line 347, in run
root=options.root_path,
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/req/req_set.py”, line 543, in install
requirement.uninstall(auto_confirm=True)
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/req/req_install.py”, line 667, in uninstall
paths_to_remove.remove(auto_confirm)
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/req/req_uninstall.py”, line 126, in remove
renames(path, new_path)
File “/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/pip/utils/__init__.py”, line 316, in renames
shutil.move(old, new)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/shutil.py”, line 258, in move
rmtree(src)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/shutil.py”, line 212, in rmtree
rmtree(fullname, ignore_errors, onerror)
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/shutil.py”, line 217, in rmtree
onerror(os.remove, fullname, sys.exc_info())
File “/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/shutil.py”, line 215, in rmtree
os.remove(fullname)
OSError: [Errno 13] Permission denied: ‘/Library/Python/2.6/site-packages/pip-6.0.8-py2.6.egg/EGG-INFO/dependency_links.txt’
Do you have a
PYTHONPATH
or some other environment variable influencing the system?when i type easy_install pip i am getting a message like this
-bash: /usr/local/bin/easy_install: /usr/local/opt/python/bin/python2.7: bad interpreter: No such file or directory.. what to do??
These instructions are very old, I recommend installing Anaconda. See https://penandpants.com/install-python/.
Error: No available formula with the name “gfortran”
GNU Fortran is part of the GCC formula:
brew install gcc