My misery in making OpenCV 2.4 work with Python 2.7 on Mac OS X Lion


I’m writing all this so that you, the reader, can hopefully learn from my mistakes and not do these steps. Or, if you have already done these kinds of things then I want to tell you that there is definitely a way to rectify all of it and make OpenCV work with Python on Mac OS X Lion.

I’d really like you to look at the blogs that are hyperlinked in this article and get a sense of other people’s mistakes so that you don’t do them. I want to really emphasize that you should be very cautious in using the codes in this article as well as those in the blogs hyperlinked here.

Getting out of my mess

My PATH variable, .bash_profile and .profile files were a total mess. I had prepended and appended a variety of folders to PATH over and over again. I looked at the unique folders within PATH and noted them down in a sperate text file. I then unset my PATH variable, deleted everything in .bash_profile and .profile files and typed the following only in my .bash_profile file

#Tell bash where to look for important things
export PATH = /opt/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:$PATH
export MANPATH=$MANPATH:/opt/local/share/man
export INFOPATH=$INFOPATH:/opt/local/share/info

#Use proxy settings on bash startup
export http_proxy=username:password@proxyURL:portNumber
export HTTP_PROXY=username:password@proxyURL:portNumber

I had installed git so I have /usr/local/git/bin in my PATH. You might not have that folder and might have other ones that ought to be in PATH. The proxy related stuff is where you would input your particular details if you use proxy. You might not need that set of codes.

After saving the .bash_profile file and sourcing it I restarted Terminal app. I checked to see if port was working by using sync and things were fine (port selfupdate never works for me because the proxy servers I am using are not configured to handle rsync). Then I looked for which python was being used as default by typing

which python

at the terminal which was using the system python installed at /usr/bin/python/

Next, I opened my .bash_profile in TextEdit app again and added the following code at the bottom in order to prepend to PATH

# Prepend PATH for locating Python 2.7 installed by Macports
PATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH

I had figured out that prepackaged installers and Macports installed python in different places. I checked each one’s version:

System python:

Sams-MacBook-Pro:~ samkhan$ /usr/bin/python -c 'import sys;print(sys.version)' 
2.7.1 (r271:86832, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

Standard python using prepackaged installer from www.python.org:

Sams-MacBook-Pro:~ samkhan$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -c 'import sys;print(sys.version)' 
2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:32:06) 
[GCC 4.0.1 (Apple Inc. build 5493)]

Latest python using Macports:

Sams-MacBook-Pro:~ samkhan$ /opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 -c 'import sys;print(sys.version)' 
2.7.3 (default, Apr 19 2012, 00:55:09) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]

It looked like the recently installed Macports version was being used as default and I confirmed it by using which python. Also, which python2.7 pointed to the same directory.

I still didn’t have openCV and I had to decide which version of python I would use for working with it. I decided to use the Macports version. I read in many places that one needs to install NumPy before installing openCV. I was also sure that I needed SciPy so I did this:

sudo port select --set python python27
sudo port install py27-numpy
sudo port install py27-scipy +universal
sudo port install opencv +python27

All this took around two hours to complete installation but then everything finally worked!

Yes, the only way to get to the light at the tunnel’s end is to keep chasing it even if there is a possibility that it could be an oncoming train.

^_^

How the mess was created

I started trying this installation on a Mac OS X Lion 10.7.4 with xcode 4.2.1 installed. I managed to make MacPorts work behind my proxy after trying on and off for six months. I had somehow managed to make OpenCV work with Python on my Mac through Macports (I really don’t remember how). However I did do one thing that might really messed with my computer.

I found this stackoverflow question while trying to figure out how to make OpenCV work with Python and trying to do the things in it made me realize that I could not locate a .bash_profile on my computer.  Then, I found some information on this blog about how to make a .bash_profile file and make OpenCV work with Python. I made it; opened it using EditText; noticed it was just empty… yes of course… I had just created it, and then I happily saved it in TextEditor after adding these environment variables:

PATH=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin:$PATH

Unfortunately, I had forgotten to do the last step in those instructions which were about sourcing the .bash_profile.

So at this point things were somehow working well. All my python codes were able to use opencv and diligently doing the things required of them. At which point I needed GamePy to try out a friend’s code. It failed to install with an error message saying that it needed system python for installation. That is when I discovered that using Macports had installed python… well… somewhere. I had no idea where Macports had installed python27.

I really don’t know why I did this but I tried to start over. I uninstalled python27 and all its dependencies including opencv @2.4.1_0+python27  using

sudo port uninstall python27 --follow-dependents installed

Somehow port stopped working right in the middle of installing python27 and also my environment variables for proxy settings disappeared. After working out the proxy problem I used the above code again. It finished without errors. So then, I grabbed an install package for python27 from www.python.org 

I installed that and just for the heck of it I restarted my computer. Then, when I tried to install py27-numpy using macports I got the error, “-sudo: port: command not found“. ( yes now I know that I’m too much of a newbie and all these operations were installing and uninstalling things in different places and messing with different system files)

After staring at the screen for a long while not knowing what to do next I decided to reinstall MacPorts through a source installation. I followed the instructions on www.macports.org and port still wouldn’t work.

After searching on the net I chanced upon this blog by bparanj which, takled about modifying the .bash_profile file to rectify the situation. I used the command

open -e .bash_profile

to edit it using TextEdit app and this time I found only this info in it:

# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH=”/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}”
export PATH

The stuff I had typed in it earlier had disappeared. So I added these lines at the top:

export PATH=$PATH:/opt/local/bin
export MANPATH=$MANPATH:/opt/local/share/man
export INFOPATH=$INFOPATH:/opt/local/share/info

and saved the file only to discover that it didn’t help. I still got the “-sudo: port: command not found” error. ( Yep, I felt totally screwed at that point)

More searching yielded another blog on “sudo-port-command-not-found”  that talked about “re-sourcing” the .bash_profile file (yeah, that last step which I had forgotten while creating and editing the .bash_profile file the first time).

So I finally got port working again by using

source .bash_profile

and I suppose

. .bash_profile

would resource the file as well. I installed py27-numpy using Macports because I need to install OpenCV. Also, after that I installed py27-scipy using Macport because I would need it eventually. It took a mighty long time though because some of the dependencies for py27-scipy are really large.

Then an hour went by while trying to install OpenCV using

sudo port select --set python python27
sudo port install opencv +python27

Finally, opencv managed to install. Then I checked if I could import cv in python by typing python2.7 at the terminal which gave me

Python 2.7.1 (Default, Jun 16 2011, 16:59:05) 
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] 
Type "help", "copyright", "credits" or "license" for more information. 
>>>

I then typed import cv at the prompt which again spat out

Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named cv

All that effort didn’t work!

And then I spent two more days futzing with my comp and there was even a time when every command in terminal stopped working because of some weird thing I did. I restarted my terminal app and things got back to normal. Macs are merely Linux PCs and if you keep screwing with system files like me, you’ll soon get Windows PC like behavior from your Mac.

Advertisements

4 thoughts on “My misery in making OpenCV 2.4 work with Python 2.7 on Mac OS X Lion

  1. Thanks for this post! Quick question, after doing all this I’m still getting a seg fault when I try to run import cv or import cv2

    It looks like this might be a pretty common problem, but I can’t figure out how to solve it. Any idea on how to fix this? Or do you know where the cv.so file is saved using the above methods by default?

    1. hi evan,

      yes this is indeed a very common problem and mostly because of the poor documentation on opencv and python sites. do have a look at this article where i try to be clearer: https://samkhan13.wordpress.com/2012/06/18/using-opencv-with-python-on-your-mac-os-x/

      according to me, copying the cv files from one place to another isn’t a good idea because you’ll eventually install other modules to enhance opencv and again run into the same problem. the above link shows all the places you could have installed python components and opencv components.

      opencv is merely a set of python modules. there are also .net, c, c++ and c# versions of it. opencv doesn’t work independently. what you really need to make sure is that your python installation is looking for the cv modules in the right place. and, numpy needs to be installed before opencv can work. for the heck of it i installed scipy as well before installing opencv.

      don’t worry, this seems to take a while but i think it was eventually worth the trouble.

      all the best,
      sam

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s