Installing Python, virtualenv, NumPy, SciPy, matplotlib and IPython on Lion

August 12, 2011 in Data,Technology

I decided to clear the cobwebs out of my  MacBook Pro (update 8/20/11: new MacBook Air in hand!) and start fresh with Lion, Python and all the other tools I regularly use (virtualenv, NumPy, SciPy, matplotlib and IPython). It was a lengthy process and I was unable to find a comprehensive walkthrough online, so I had to figure it out through trial and error (mostly error). I was surprised at the lack of available information; with such a major release I expected more complete documentation. So, once again quoting Tom Lehrer, I have a modest example here.

The important things we'll accomplish in this guide are (click any section to jump directly there):

  1. A clean install of Mac OS 10.7 Lion
  2. Installing Homebrew
  3. Installing Python (and pip)
  4. Installing virtualenv
  5. Installing NumPy, SciPy and matplotlib
  6. Installing IPython and the qtconsole

As you may recall, this has been a somewhat more-than-trivial process for Lion's early adopters, as enough was changed behind the scenes to mess up some of the current versions of these tools. I'll detail how to get around those issues by getting cutting-edge distributions. Note that today's cutting-edge works on my machine; tomorrow's may introduce new bugs. When stable Lion-compatible versions of these programs are available, I strongly encourage you to use those instead.

If you would rather skip my commentary, you should be able to simply execute every line of code in sequence.

As always, remember that some of these lines will take you off the beaten path and could cause serious irrevocable damage to your computer! In particular, typing sudo gives you root access and therefore the ability to erase everything with a single keystroke. Please do not execute any code without understanding exactly what it does. In the worst case, I suppose you could always jump back to square one by wiping your hard drive and running a Lion clean install, but use caution nonetheless. YMMV.

Lion Clean Install

To begin, there has been some concern that users can't perform a clean install of Mac OS X 10.7 (Lion) without quite a bit of hacking around with physical media. I'm happy to report that isn't true -- as long as you performed a standard Lion install first. The OSX installer (App Store link) automatically creates a "Recovery HD" partition on your hard drive, just in case something goes wrong. The partition contains just two things: Disk Utility  and the ability to run the Lion installer. To access this recovery mode, hold Command-R while your computer is starting up. Use Disk Utility to erase your hard drive, then run the installer to re-download and load Lion. When you're done, you'll have a clean install of Lion. Download your favorite browser and you're good to go. I have to say, discovering this feature was a pleasant surprise -- hats off to Apple for thinking ahead on this one.

Next, install Xcode 4 -- its bundled compilers have to be available for the next step to work. Here's a link to it in the App Store; it's a free (but large) download. Note that like Lion, the download does not actually install the application. Instead, it puts an "Install Xcode" icon in your applications folder, which you must run to complete the installation. I don't understand this behavior of installing the installer, but it is what it is.


Homebrew

Next up, one of my new favorite tools -- Homebrew. The homepage says it pretty well: "Homebrew is the easiest and most flexible way to install the UNIX tools Apple didn't include with OS X." If you've messed around with MacPorts (or tried to do this sort of stuff  -- gasp -- by yourself), you'll be shocked by the drop-dead simplicity of this tool. Even installing it is a no-brainer; just fire up Terminal and execute the following line:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"

That's it -- now you have Homebrew. Try running brew update and brew doctor at the command line to make sure it's working properly.

You'll need to add the Homebrew directory to your system path, in order to make sure that Homebrew-installed software is given higher priority than any other version. To do so, open your .bash_profile in your user directory (that's the ~ directory or, more verbosely, /Users/[your_user_name]) and add the following line (if you don't have a .bash_profile, create one with your favorite text editor -- you may need to do this in order to see it, as it will be a hidden file):

export PATH=/usr/local/bin:$PATH

If you haven't already, restart Terminal so it picks up the new path. Homebrew is generally very good about alerting you to any action you need to take after it finishes running, including path modifications. Make sure to pay attention to its output.

Python

Now on to the main event: Python. Let's start by installing some prerequisites (the last one is a prerequisite for IPython, but we might as well get it with the others):

brew install readline sqlite gdbm pkg-config

And now Python itself. As of this writing, 2.7.2 is the most recent version. There are some version-specific references in the following instructions; change them as necessary. The --framework option tells it to build as a Framework, which has some downstream niceties, and --universal builds a universal (32/64 bit) version:

brew install python --framework --universal

This will install a version of disutils, but you must update your path in order to run it properly. Add the following to your .bash_profile (you may combine it with the previous .bash_profile edit, if you choose):

export PATH=/usr/local/share/python:$PATH

Once again, make sure you reload Terminal to update your path variable. Finally, you need to change Lion's symlink to point at your new Python install. Run the following three lines in sequence:

cd /System/Library/Frameworks/Python.framework/Versions
sudo rm Current
ln -s /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/Current

You will be prompted for your password after executing the sudo command.

To verify that your installation went as planned, type which python at the command line. You should see /usr/local/bin/python in response. Furthermore, if you type python, you should see Python 2.7.2 launch (type quit() to return to Terminal).

You already have easy_install available through Homebrew's version of disutils, but pip, in my opinion, is a much better package manager -- not least because it allows simple uninstalls. To get pip, simply execute:

easy_install pip

Note that depending on your permissions settings, you may have to prepend sudo to the front of many of these lines. Always remember that sudo gives you root access!! Therefore, in the interest of caution, I'm not going to include the sudo command here, but if your installs are failing because of permissions, you may have to add it.

virtualenv

Virtualenv is a tool that allows you to create isolated Python environments, each with its own set of packages and dependencies. This is useful for testing or managing package requirements (for example, if you build an application that is dependent on a certain version of a third-party package but another application requires a more recent version, you may break the first application by upgrading). This is not a required step, and all the commands below should work whether or not you are using virtualenv, so I'm listing it here for convenience only. The only difference will be that directories (such as that returned by which pip) will point to the virtualenv rather than /usr/local.

First, install virtualenv and virtualenvwrapper (a tool which makes working with virtualenv somewhat easier):

pip install virtualenv
pip install virtualenvwrapper

Next, source the virtualenvwrapper script:

source /usr/local/share/python/virtualenvwrapper.sh

This will create a hidden virtualenv directory at ~/.virtualenv. Now you can make your first virtual environment:

mkvirtualenv test1

Your new virtualenv test1 comes with a complete install of Python 2.7.2 and its own version of Pip. It is activated by default, so running any pip command will only impact this environment. Note that if you deactivate the virtualenv, you will lose access to any packages installed in it. You can switch between virtualenvs with the workon command.

In order to delete your test virtualenv, run rmvirtualenv test1. Also, to specify a different version of Python, just use mkvirtualenv -p python3.2 test1 (replacing python3.2 with the shell name of your preferred version).

NumPy

If pip installed properly (executing which pip should return /usr/local/share/python/pip), then you can install NumPy simply with:

pip install numpy

Check your work by executing:

python

followed by:

import numpy
print numpy.__version__
print numpy.__file__
quit()

The version should be 1.6.1 (as of this writing), and the file should be somewhere in /usr/local/Cellar/....

SciPy

SciPy requires a Fortran compiler, which Lion lacks. To acquire one, use Homebrew:

brew install gfortran

As of this writing, the stable version of SciPy (0.9) will not compile properly under Lion. If you try installing SciPy with pip, it will fail. Fortunately, a fix has already been committed to the dev version (0.10) and we can instruct pip to use that version:

pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev

As with NumPy, make sure you have everything working by running:

python

and then:

import scipy
print scipy.__version__
print scipy.__file__
quit()

Technically speaking, it still isn't compiling properly -- but this should run well enough for most work. If you're a stickler for these things, then you can force SciPy to compile using gcc 4.2, rather than Lion's default llvm-gcc 4.2, by reassigning the CC shell variable to the appropriate compiler and building the package. Note that if you want to do this with Homebrew, you'll need to adjust the symlinks to cc, gcc, g++ and c++ in /usr/bin to point at gcc-4.2 (and its variants as appropriate) rather than their current targets, because the Homebrew script calls the symlink rather than the shell variable. I'm going to assume that if you choose to go this route, you also have the know-how to do so, but feel free to email me otherwise. Note that this method isn't necessarily "correct" and I'm not a SciPy developer. Most likely, this will all be handled automatically by the final 0.10 release.

matplotlib

Like SciPy, matplotlib doesn't play nicely with Lion (due to an incompatibility with libpng 1.5). By this point, you know the drill -- just point pip at the patched development version, and check its work when it finishes:

pip install -e git+https://github.com/matplotlib/matplotlib#egg=matplotlib-dev

IPython

Installing IPython itself is a fairly straightforward pip command:

pip install ipython

Getting the new qtconsole to run takes a little more work -- but it's well worth it. The qtconsole is like running IPython on steroids -- my favorite feature is that matplotlib output can be displayed inline.

To begin, you'll need Nokia's qt library, which is available here. Make sure you download the library, not the SDK.

Once that's done, begin installing the prerequisites:

brew install pyqt

Note that after installing pyqt, brew will prompt you to add the following to your pythonpath in .bash_profile:

export PYTHONPATH=/usr/local/lib/python:$PYTHONPATH

Continue installing:

brew install zmq
pip install pyzmq
pip install pygments

And that's it -- you should now be able to launch the qtconsole by executing ipython qtconsole. If you want to see the matplotlib output inline (and why wouldn't you?), then execute:

ipython qtconsole --pylab=inline

Fin.

Phew! I hope this works for everyone out there. Until the next software upgrade, anyway.

Updated 9/1/11 to include virtualenv.

{ 56 comments… read them below or add one }

Patrick August 19, 2011 at 4:35 am

Thanks, great howto! Starting to really like this Homebrew stuff. One question though: why do you install Python as a universal? Wasn’t 32-bit supposed to get the boot by now?

Reply

J August 20, 2011 at 2:01 am

Thanks, Patrick! To your question, I’m not sure if there’s a good reason not to do so, as long as the option is available, to ensure backwards compatibility. For example, I think matplotlib still runs 32-bit. (I could be wrong about that.)

Reply

Min August 20, 2011 at 12:39 pm

You are *sort of* wrong about matplotlib. It depends on the GUI toolkit you are using. Matplotlib itself will generally be built universal (or otherwise matching the Python that built it). However, the stable Wx binary install is 32-bit only, so if matplotlib is run with the wx backend, it will be 32b. If, however, you are using the native Cocoa (aka backend=’MacOSX’), then it will be able to run in either 32 or 64b, and all Qt binaries I have seen are also 32+64b.

Reply

J August 22, 2011 at 1:34 pm

Ah, fair enough.

Reply

Juan Delard August 21, 2011 at 5:59 am

Thanks… you solved all my mac/python/scipy problems in one clear tutorial! Cheers

Reply

J August 22, 2011 at 1:33 pm

Juan, I’m glad I could help.

Reply

Christian August 22, 2011 at 12:03 pm

If anyone else is getting errors trying to install pyqt with homebrew on Lion, I found that you have to link

/Library/Frameworks/Python.framework/Versions/Current

just like you did with

/System/Library/Frameworks/Python.framework/Versions/Current.

That is:
cd /Library/Frameworks/Python.framework/Versions
sudo rm Current
ln -s /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/Current

Reply

J August 22, 2011 at 1:34 pm

Thanks for letting us know, Christian

Reply

Adam November 3, 2011 at 11:24 pm

Same issue. Otherwise tutorial was dead on. Thanks!

Reply

Justin August 23, 2011 at 6:48 pm

Thanks for this tutorial, i thought it worked well. But I have one issue: I am missing a lot of functionality of scipy. For instance scipy.special and scipy.optimize are missing. There exist in the folder scipy/, but are not accessible from python. Any suggestions would be appreciated.

Reply

J August 24, 2011 at 12:14 am

They seem to work ok for me — are you sure you are importing them before you try to access their functions?

For example –

import scipy
scipy.optimize.newton() #fails
import scipy.optimize
scipy.optimize.newton() #works

Reply

macdental August 25, 2011 at 4:31 pm

Thank you so much!!!!! I was deeply troubled by the failure to install many Python modules on Lion. Your post helped so much!! Thank you.

Reply

Luca August 26, 2011 at 4:19 am

since I haven’t ever used Nokia’s qt library, might you courteously tell me what is the right version?
1) http://get.qt.nokia.com/qt/source/qt-mac-opensource-4.7.3.dmg
or
2) http://get.qt.nokia.com/qt/source/qt-mac-opensource-4.7.3-debug-libs.dmg
Thanks in advance for the informations you’ll be able to give me.

Reply

J September 15, 2011 at 4:13 am

Luca, sorry for the delayed reply. You want the first one (should be a little more than 200 MB).

Reply

Michael Stroeck August 26, 2011 at 12:08 pm

FYI, I had to manually install this package:

http://developer.qt.nokia.com/wiki/PySide_Binaries_MacOSX

Otherwise, “ipython qtconsole” would not start.

Reply

macdental August 26, 2011 at 12:19 pm

I did it too. Somehow the Qt framework was not properly found either (although it does sit in /usr/..); I manually installed it too. Everything then works.

Reply

Sami Spjuth August 27, 2011 at 5:10 pm

Many many thanks! Worked like a charm.

Reply

James Eastland September 8, 2011 at 4:09 pm

Thanks so much. Easily the most helpful post I’ve read in a long time. I was able to painlessly accomplish in an evening what probably would have involved days of frustration otherwise.

Reply

fwix September 14, 2011 at 10:33 am

Hi, I’m quite new in mac os universe (coming from debian and aptitude world).
Thanks for this usefull article that helped me to reinstall a usable python environment. But, I have a total noob question : how to install the wich command on macos ?

Reply

J September 15, 2011 at 12:56 am

Hi — the which command should be built right in, just try which python in your terminal.

Reply

Dolan October 1, 2011 at 7:51 am

I’m running into some problems with my install, which may be because I’m not starting with a fresh install of Lion (i.e. at some point or another, I’ve manually installed stuff like python, pip, easy_install, but without homebrew).

When I install python via homebrew without sudo, I get a some errors with a final permission error at end of install:

At the beginning of ‘make install’, at about 10 outlines in, I see:
install: /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/Python.framework/Versions/2.7/Python: No such file or directory

Shortly after this, I see a bunch of errors (100′s of lines) similar this:
/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Mac/Modules/fm/_Fmmodule.c:26: error: expected ‘)’ before ‘*’ token
/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Mac/Modules/fm/_Fmmodule.c: In function ‘Fm_GetFontName’:

These errors end with:
lipo: can’t open input file: /var/folders/8b/n0j5pn_13qn_x8p2v4f848zh0000gn/T//cc1jDrL0.out (No such file or directory)
Python build finished, but the necessary bits to build these modules were not found:
_bsddb dl imageop
linuxaudiodev ossaudiodev spwd
sunaudiodev
To find the necessary bits, look in setup.py in detect_modules() for the module’s name.
Failed to build these modules:
_Fm _Qd _Qdoffs

Then “running build_scripts” starts, which has no errors until:
building ‘_Fm’ extension
/usr/bin/llvm-gcc -fno-strict-aliasing -fno-common -dynamic -arch i386 -arch x86_64 -O3 -march=core2 -w -pipe -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Mac/Include -I. -IInclude -I./Include -I/usr/local/Cellar/readline/6.2.1/include -I/usr/local/include -I/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Include -I/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2 -c /private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Mac/Modules/fm/_Fmmodule.c -o build/temp.macosx-10.5-intel-2.7/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Mac/Modules/fm/_Fmmodule.o -Wno-deprecated-declarations
/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Mac/Modules/fm/_Fmmodule.c:26: error: expected ‘)’ before ‘*’ token
/private/tmp/homebrew-python-2.7.2-aVM7/Python-2.7.2/Mac/Modules/fm/_Fmmodule.c: In function ‘Fm_GetFontName’:

Next “running install_lib” starts with no errors, then finally terminates with:
OSError: [Errno 13] Permission denied: ‘/Applications/Python 2.7/Build Applet.app/Contents/Info.plist’
make[1]: *** [install_BuildApplet] Error 1

Here’s the additional details provided at the end:
==> Environment
HOMEBREW_VERSION: 0.8
HEAD: (none)
HOMEBREW_PREFIX: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_REPOSITORY: /usr/local
HOMEBREW_LIBRARY_PATH: /usr/local/Library/Homebrew
Hardware: 8-core 64-bit sandybridge
OS X: 10.7.1
Kernel Architecture: x86_64
Ruby: 1.8.7-249
/usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
Xcode: 4.1
GCC-4.0: N/A
GCC-4.2: build 5666
LLVM: build 2335
MacPorts or Fink? false
X11 installed? true
==> Build Flags
CC: /usr/bin/llvm-gcc => /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
CXX: /usr/bin/llvm-g++ => /usr/llvm-gcc-4.2/bin/llvm-g++-4.2
LD: /usr/bin/llvm-gcc => /usr/llvm-gcc-4.2/bin/llvm-gcc-4.2
CFLAGS: -O3 -march=core2 -w -pipe
CXXFLAGS: -O3 -march=core2 -w -pipe
CPPFLAGS: -I/usr/local/Cellar/readline/6.2.1/include
LDFLAGS: -L/usr/local/Cellar/readline/6.2.1/lib
PKG_CONFIG_PATH: /usr/local/Cellar/readline/6.2.1/lib/pkgconfig

Any help/ideas is greatly appreciated!

Reply

Dolan October 1, 2011 at 7:57 am

I forgot to mention, if I install homebrew’s python via sudo, python installed okay, but later my pip and/or easy_install were loading the wrong versions. Instead of from /usr/local/share/python, it wanted to user /usr/local/bin. When I ran the /usr/local/share/python version directly, installing numpy failed, saying it was already installed. When I tried an pip upgrade of numpy, it returned a bunch of errors (I think it was trying to use an old sites-packages location). Importing numpy via python was failing though, so I know I was using the homebrew version of python (saw 2.7.2 in shell). After all this weirdness, I decided to start over w/out sudo, since that is what homebrew is supposed to allow. This is what brought me to my above post’s results :(

Reply

J October 1, 2011 at 2:41 pm

Hi Dolan — Unfortunately I’m not sure what is causing your problems. Your second comment makes me wonder if the problem is with your $PATH variable —

Do you have export PATH=/usr/local/share/python:$PATH in your .bash_profile? If so, running which pip should show the one in /usr/local/share/python. You could type echo $PATH to see the $PATH as it is set currently.

Also, it could be that you have some permissioning problems on /usr/local — if that’s the case, even Homebrew won’t have full access.

I’m sorry I can’t be more helpful — I’d advise you to start from the top and make sure that every time Homebrew prompts you to adjust the PATH, it gets updated in .bash_profile!

Reply

Dolan October 1, 2011 at 9:22 pm

Hi J, thanks for the feedback.

Regard my PATH, I believe I got all the steps implemented, and made sure the var was updating before running the commands as needed. Here’s my PATH: /usr/local/bin:/usr/local/share/python:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Developer/usr/bin

Regarding /usr/local permissions, here are the items that are owned by my account: Cellar, Library, include. The rest (e.g. bin, etc, git, lib, man, share) are owned by root.

Regarding sudo vs. non-sudo install, I don’t have a strong preference either way, as long as in the end I get scipy, etc working. I wonder if there is a “simple” way to get my box close to a fresh install (i.e.

Reply

J October 2, 2011 at 2:12 am

Ok — I think this is part of the problem. Your PATH is searched in order, so because it has /usr/local/bin before use/local/share/python, it picks up the files in /usr/local/bin first.

Somewhere, you system is injecting /usr/local/bin back into your PATH after you put /usr/local/share at the top. It could be in your .bash_profile — check the order of the lines that edit your PATH — or potentially some application is doing it?

Reply

rise October 30, 2011 at 7:52 pm

I installed both Python 2.7.2 and Python 3.2.2 which are located in /usr/local/Cellar/ .

I installed virtualenv and virtualenvwrapper and noticed that the file, virtualenvwrapper.sh, was installed in /usr/local/share/python3/virtualenvwrapper.sh set to 755.

I entered the following at the command prompt at this part of your tutorial and received the following error msg:
chucky-bear:~ rise$ source /usr/local/share/python/virtualenvwrapper.sh
-bash: /usr/local/share/python/virtualenvwrapper.sh: No such file or directory

So, I tried this line of info and got the following:
chucky-bear:~ rise$ source /usr/local/share/python3/virtualenvwrapper.sh
Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named virtualenvwrapper.hook_loader
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python and that PATH is set properly.

Here is my env info:
chucky-bear:~ rise$ env
TERM_PROGRAM=Apple_Terminal
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/n5/w6y129l92xs3v5n133lkxh8r0000gn/T/
Apple_PubSub_Socket_Render=/tmp/launch-rqBpAm/Render
TERM_PROGRAM_VERSION=299
TERM_SESSION_ID=28ABDF94-0062-4DF0-B490-98E16ABD5BCC
USER=rise
COMMAND_MODE=unix2003
SSH_AUTH_SOCK=/tmp/launch-qIqWHA/Listeners
__CF_USER_TEXT_ENCODING=0x1F5:0:0
WORKON_HOME=/Users/rise/.virtualenvs
PATH=/usr/local/share/python3:/usr/local/share/python:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
VIRTUALENVWRAPPER_HOOK_DIR=/Users/rise/.virtualenvs
PWD=/Users/rise
LANG=en_US.UTF-8
SHLVL=1
HOME=/Users/rise
LOGNAME=rise
DISPLAY=/tmp/launch-5ielYj/org.x:0
VIRTUALENVWRAPPER_LOG_DIR=/Users/rise/.virtualenvs
_=/usr/bin/env

I noticed that I have a ~/.virtualenvs folder.

I am no expert at the Mac…. Could you please assist me in why it is giving the error msg for the source /usr/local/share/python/virtualenvwrapper.sh ?

Thnx much in advance…

Reply

rise October 30, 2011 at 8:41 pm

Hi, Well, I am not sure if I resolved this issue or not. But I went to this guy’s website: http://www.insomnihack.com/?p=442
and albeit he is on Snow Leopard, I thought that I would give a try with his info in his bash ~/.profile .

I used the ~/.bash_profile document and added this line to it as the above website mentioned:
# virtualenvwrapper
export WORKON_HOME=$HOME/VirtualEnvs
if [ -f /usr/local/bin/virtualenvwrapper.sh ]; then
source /usr/local/bin/virtualenvwrapper.sh
fi

I get the following error msg when I try “mkvirtualenv test1″ at the command prompt:
chucky-bear:~ rise$ mkvirtualenv test1
-bash: mkvirtualenv: command not found

However, when I enter the following respectively at the command prompt:
>> python or >>python3.2 or >>python2.7, then that version of python will run.

I’ll keep playing with it to see if I can continue with your tut…Thnx much…

Reply

J November 1, 2011 at 5:34 am

Have you had any further luck? I am not too familiar with the details of troubleshooting virtualenv, so I’m not sure what to say about your errors. Perhaps someone else here can help?

Reply

Maylon November 2, 2011 at 12:52 pm

Hey all,

I can’t seem to get the pip to install in the /usr/local/share/python/ directory. No matter what I do with either .bashrc or .bash_profile, it always installs in /usr/local/bin directory.

As a result the rest of the installations all end up in the same directory. Any advice?

Reply

Statto November 7, 2011 at 7:38 am

Great tutorial, thanks! Does nearly everything I need, but I was wondering if you had any suggestions for getting Visual Python up and running under Lion… I don’t think either pip or easy_install support it at present…any ideas? :)

Reply

J November 7, 2011 at 9:17 am

Sorry, Statto, I’m not familiar with VPython. It looks like the only option is to download the installer… if that doesn’t work I’m not sure what to suggest.

Reply

Prashanth Rao December 9, 2011 at 10:05 am

Awesome tutorial. I used it to install all of these components on a 10.6.8 system. Thanks again.

Reply

David Wolf December 16, 2011 at 11:51 pm

Hello, i was looking for a tutorial like this the whole week… thanks a lot, I have a concern, after running “the brew doctor” I got this message:

MacBook-Pro-de-David:~ David_Wolf$ brew doctor
We couldn’t detect gcc 4.2.x. Some formulae require this compiler.
NOTE: Versions of XCode newer than 4.2 don’t include gcc 4.2.x.

==> /usr/bin occurs before /usr/local/bin
This means that system-provided programs will be used instead of those
provided by Homebrew. This is an issue if you eg. brew installed Python.

Consider editing your .bashrc to put:
/usr/local/bin
ahead of /usr/bin in your PATH.
Some “config” scripts were found in your path, but not in system or Homebrew folders.

`./configure` scripts often look for *-config scripts to determine if software packages
are installed, and what additional flags to use when compiling and linking.

Having additional scripts in your path can confuse software installed via Homebrew if
the config script overrides a system or Homebrew provided script of the same name.

/Library/Frameworks/Python.framework/Versions/2.7/bin
python-config python2.7-config

Will that be a major issue? (We couldn’t detect gcc 4.2.x.) I just installed the new version which is Xcode 4.2.1 today so it is update to this date. So do I need to get the gcc4.2.x???
I will continue with the steps ’cause i need to have it operational for Monday, but if you could solve my doubt would be nice.

Thanks!

Reply

J January 11, 2012 at 8:39 pm

Hi David — I’m very sorry about the late reply. In my experience, I think you’re fine to continue — however this isn’t within my expertise.

Reply

Nevin January 1, 2012 at 9:09 pm

This was a great article and I thank you very much for it.

I am getting segmentation fault: 11 errors when running the ipython qtconsole as described in the last command, though. I’m not sure why. The only hitch i had in following these steps was with scipy, where I noticed I installed the development version 0.11 of scipy rather than .10 described. I wasnt using the module though so I’m not sure the error is related.

Thoughts?

Reply

J January 11, 2012 at 8:40 pm

Sorry Nevin, I haven’t had that problem! WRT Scipy, version 10 has been released, so your experience is now normal.

Reply

Matt January 10, 2012 at 5:58 pm

I used pretty much this exact procedure except that I didn’t build Python with –framework –universal. I’ve encountered the matplotlib problem described here: https://github.com/matplotlib/matplotlib/issues/665 and I’m curious if you or your readers have seen anything similar.

Reply

J January 11, 2012 at 8:41 pm

Sorry Matt, I haven’t had that problem. Maybe someone else here has?

Reply

Stuart January 11, 2012 at 11:03 pm

Thanks for these detailed instructions. Three difficulties arose on my macbook core 2 duo circa 2008:

1). For some reason, the dev version of scipy was un-importable after a successful install. I fixed this with
(pip uninstall -v scipy; pip install -v scipy), removing the dev version and installing the standard release.

2). Pyqt installation failed because it was linking to the wrong python libraries. In particular, an outdated sip was linking to python 2.7.1 (missing!), whereas 2.7.2 should have been used. And, I checked that my paths were correct. Once I realized that sip was responsible for the incorrect links, I simply wiped out the old versions and re-installed (brew remove –force sip; brew install -v sip). For reference, the first sign of error was:

> qpyopengl_uniform_value_array.cpp:26:20: error: Python.h: No such file or directory

3). The second problem occurred later in compiling pyqt. I’m still puzzled. Nonetheless, a solution was to re-configure pyqt with an additional flag “–no-designer-plugin”, by editing $(brew –prefix)/Library/Formula/pyqt.rb, and forcing a re-install (i.e. brew install –force -v pyqt). Hats off to Phil Thompson here: http://python.6.n6.nabble.com/make-failure-on-mac-os-x-td1925137.html. In this case, the error was:

> ld: warning: ignoring file /Library/Frameworks//Python.framework/Python, missing required architecture x86_64 in file
> Undefined symbols for architecture x86_64:
> “_Py_IsInitialized”, referenced from:
> PyCustomWidgets::PyCustomWidgets(QObject*)in pluginloader.o

It was worth the effort!
- Stu

Reply

Josh January 19, 2012 at 7:34 am

Hey!

This tutorial worked, except that after closing my terminal i’m not anymore in the virtualenv. Being a total noob, i’d like to know :
- how i could access my virtualenv;
- how can i set up this virtualenv as the default one.

Thank you!

Reply

J January 19, 2012 at 11:20 am

Hi Josh — did you get virtualenvwrapper? If so, just type “workon [your venv name]” and you’ll be right back in it.

Reply

Josh January 20, 2012 at 7:01 am

Oh I found a way to make it work, but in order to workon to be recognized as a command, i need to first type “source /usr/local/share/python/virtualenvwrapper.sh” (as in the tutorial). Any way to avoid that?
Thanks!

Reply

J January 20, 2012 at 9:41 am

Yup — just add these lines to the .bash_profile that you created or updated during this tutorial, and I think you should be set. The only caveat is that these are “default” instructions for the wrapper, so make sure you first change them to reflect your specific setup (e.g. /usr/local/share/python in the third command):

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh

Basically, this tells Terminal to execute those lines every time it starts, saving you the trouble. Let us know if it works!

Reply

(yet another) Josh January 20, 2012 at 11:32 am

J, I send you good karma for responding to Josh so helpfully.

The way he asked his question reminded me of my struggle to learn the *NIX underpinnings of my Mac last year. I’m pretty comfortable with it all now, but it was tough to find resources that were relevant, and/or not written by and for SysAdmins. So I’m extra sympathetic to command-line newbies, and would have loved more answers like yours!

Reply

Josh January 24, 2012 at 9:28 am

Thank you very much J. Now it’s working. Thank you again!

Reply

PeterVK January 21, 2012 at 7:39 am

matplotlib 1.1.0 is available. I was able to successfully install it with:

pip install -f http://garr.dl.sourceforge.net/project/matplotlib/matplotlib/matplotlib-1.1.0/matplotlib-1.1.0.tar.gz matplotlib

without the explicit location of the tar, pip installs version 1.0.1

Reply

Samuel January 26, 2012 at 1:43 pm

Compiling scipy with llvm-gcc (which is what `pip install scipy` does) leads to a segfault (at least on my system) when running scipy.test().

scipy.test()
Running unit tests for scipy
NumPy version 1.6.1
NumPy is installed in /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy
SciPy version 0.10.0
SciPy is installed in /usr/local/Cellar/python/2.7.2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/scipy
Python version 2.7.2 (default, Jan 13 2012, 15:00:18) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)]
nose version 1.1.2
……………………………………………………………………………………………………………………………………………………………..FSegmentation fault: 11

I did some work to avoid the currently present problem of building scipy with pip
on OS X Lion because of the llvm-gcc and gfortran issue.

Here we go:
/usr/bin/ruby -e “$(curl -fsSL https://raw.github.com/gist/323731)”
brew install gfortran
brew install https://raw.github.com/samueljohn/homebrew-alt/samuel/duplicates/numpy.rb
brew install https://raw.github.com/samueljohn/homebrew-alt/samuel/other/scipy.rb

Then to install matplotlib:
cd to your site-packages:
pip install -v -e git+https://github.com/matplotlib/matplotlib#egg=matplotlib-dev

A pity that pip install numpy, pip install scipy and pip install matplotlib seems to be broken.

Reply

J January 26, 2012 at 1:55 pm

I can confirm that I get the segfault on my machine as well when running scipy.test(). A quick search seems to show that this problem has existed for a little while. However, I will say I have not had any issues using scipy itself.

Reply

Gary January 31, 2012 at 9:09 pm

Hello,

I can’t seem to get past this step when install virtualenv:

source /usr/local/share/python/virtualenvwrapper.sh

Traceback (most recent call last):
File “”, line 1, in
File “/Library/Python/2.7/site-packages/virtualenvwrapper/hook_loader.py”, line 16, in
import pkg_resources
ImportError: No module named pkg_resources
virtualenvwrapper.sh: There was a problem running the initialization hooks. If Python could not import the module virtualenvwrapper.hook_loader, check that virtualenv has been installed for VIRTUALENVWRAPPER_PYTHON=/Library/Frameworks/Python.framework/Versions/2.7/bin/python and that PATH is set properly.

Not at all sure how to fix this problem.
I am very new to Mac OSX so any help would be greatly appreciated.

Reply

J January 31, 2012 at 10:02 pm

Gary — unfortunately this problem seems to be very specific to your machine. It appears that some of your directories have become confused. You could try installing pkg_resources directly (“pip install pkg_resources”) or verifying that virtualenv is indeed in the /usr/local/share/python directory.

I don’t use virtualenv regularly so I’m not sure how to advise you. You may have better luck reaching out to the virtualenvwrapper community directly.

Reply

Itzel February 10, 2012 at 12:29 pm

Thank you !!! It was very useful I learned a lot thanks =D

Reply

Claudio February 10, 2012 at 1:44 pm

Thank you for this tutorial! It saved my day.

Reply

Fabian February 18, 2012 at 10:00 pm

I solved many of the above problems installing the Command Line Tools in Xcode 4.3. See: https://github.com/mxcl/homebrew/issues/10244

Reply

William Jennings February 19, 2012 at 11:18 pm

I feel quite embarrassed that I’ve gone through 2 days of trying to get to get this to work. I’ve scraped through github forums, stackoverflow and all the links that can bide me some type of logic. Yet, alas I still fail wildly with this set of errors:
In file included from src/backend_agg.cpp:11:
In file included from src/_backend_agg.h:34:
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
—————————————-
Command /Users/Will/.virtualenvs/test1/bin/python -c “import setuptools; __file__=’/Users/Will/.virtualenvs/test1/src/matplotlib/setup.py’; exec(compile(open(__file__).read().replace(‘\r\n’, ‘\n’), __file__, ‘exec’))” develop –no-deps failed with error code 1 in /Users/Will/.virtualenvs/test1/src/matplotlib
Storing complete log in /Users/Will/.pip/pip.log

Reply

William Jennings February 19, 2012 at 11:18 pm

That is all regarding matplotlib – sorry for not saying.

Reply

Bob Lewis February 20, 2012 at 7:08 pm

As of yesterday, this advice worked for me. The only glitch was that the scipy install complained about various headers associated with the “umf” library. After a “brew install umfpack”, the rest of the installs completed without problems.

Thanks!

Reply

Leave a Comment

{ 5 trackbacks }

Previous post:

Next post: