12

When trying to "import cv" in python I get: "ImportError: No module named cv". This question has appeared in many forms, but I haven't found the answer that helps.

On my Mac OS X Lion, among many other ports, I have run:

sudo port install python27

and

sudo port install opencv +python27

Running:

port select python

I see: "python27 (active)"

And running:

port installed opencv

I see: "opencv @2.3.1a_1+python27 (active)"

What else should I check? Thanks.

jensph
  • 763
  • 1
  • 10
  • 22
  • 1
    Should I find cv.so in: `/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/`? It's not there... – jensph Dec 09 '11 at 17:04

3 Answers3

20

I had this same problem. It looks like a (maybe?) bug with the OpenCV install script for 2.3.1a. It will not create the Python bindings unless NumPy is already installed.

To fix it:

sudo port uninstall opencv
sudo port install py27-numpy
sudo port install opencv +python27

That worked for me! I found this by doing some Googling, and the correct answer was here: wbarczynski.org/wp/2011/11/25/opencv-with-py-bindings-on-mac-with-macports-intall-numpy-first/

Tom Morris
  • 10,490
  • 32
  • 53
Pete
  • 216
  • 1
  • 2
  • 1
    Thanks a lot; this worked! In particular I ran: `sudo port uninstall opencv @2.3.1a_1+python27` `sudo port uninstall opencv @2.3.1a_1` `sudo port install py27-numpy` `sudo port install opencv +python27` – jensph Jan 08 '12 at 21:33
  • I was then able to run `import cv` in python. – jensph Jan 08 '12 at 21:40
  • 2
    I did port install py27-numpy – user391339 Oct 03 '12 at 05:11
  • 1
    This also worked for me. For me, I installed numpy using both macports and pip before. So that might be causing the problem. So I uninstalled numpy with pip. Then did what was suggested and everything worked. Thanks. – pyan Nov 11 '15 at 20:03
  • Interesting. @pyan, it worked for me to (1) install numpy and cv2 with pip, (2) install opencv 3.1 with the Python 2.7 bindings. I did not have to replace the pip-based numpy by a MacPorts one. – Eric Platon Mar 21 '16 at 03:37
2

STEP ONE

Use Macports to install opencv.

STEP TWO

Put these two files somewhere on your PYTHONPATH (for example in your site-packages):

STEP THREE

Create the below soft links (because cv2.so expects the dylibs to be in /usr/local but MacPorts installs to /opt/local):

cd /usr/local/lib/
ln -s /opt/local/lib/libopencv_core.2.3.dylib libopencv_core.2.3.dylib
ln -s /opt/local/lib/libopencv_flann.2.3.dylib libopencv_flann.2.3.dylib
ln -s /opt/local/lib/libopencv_imgproc.2.3.dylib libopencv_imgproc.2.3.dylib
ln -s /opt/local/lib/libopencv_video.2.3.dylib libopencv_video.2.3.dylib
ln -s /opt/local/lib/libopencv_ml.2.3.dylib libopencv_ml.2.3.dylib
ln -s /opt/local/lib/libopencv_features2d.2.3.dylib libopencv_features2d.2.3.dylib
ln -s /opt/local/lib/libopencv_highgui.2.3.dylib libopencv_highgui.2.3.dylib
ln -s /opt/local/lib/libopencv_calib3d.2.3.dylib libopencv_calib3d.2.3.dylib
ln -s /opt/local/lib/libopencv_objdetect.2.3.dylib libopencv_objdetect.2.3.dylib
ln -s /opt/local/lib/libopencv_legacy.2.3.dylib libopencv_legacy.2.3.dylib
ln -s /opt/local/lib/libopencv_contrib.2.3.dylib libopencv_contrib.2.3.dylib
AlcubierreDrive
  • 3,654
  • 2
  • 29
  • 45
  • I did _not_ need to carry out steps two and three in order to get the python bindings working properly. Thanks. – jensph Jan 08 '12 at 21:43
  • 1
    Hm. I wonder why my environment required that. Surely some prior install of some conflict. – AlcubierreDrive Jan 10 '12 at 10:15
  • I followed your suggestions above, and I am not getting an ImportError. Unfortunately the opencv module is failing (Process finished with exit code 139). – user391339 Oct 03 '12 at 06:38
  • I did a try: import cv2 except ImportError: print "cv2 is not installed" exit() print "looks like it is installed" exit() but it crashed with the error 139 without giving any output whatsoever. – user391339 Oct 03 '12 at 06:45
-2

I was wondering if you have installed opencv to the OSX version of the python instead of macports.

have you tried easy install, I normally use easy_install to install the packages.

/opt/local/bin/easy_install-2.7 opencv
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
  • I do see all the libopencv_*.dylib files in /opt/local/lib, which I believe is where MacPorts would have installed them. Would installing using your suggestion write over or conflict with the MacPorts install? And would easy_install include the python bindings? Thanks. – jensph Dec 07 '11 at 23:52
  • so its your choice, to pick which python version you want to use. I use MacPorts installation. Respect to python bindings I haven't had any problem using any packages after installing as I mentioned above. – add-semi-colons Dec 08 '11 at 01:50
  • I have only been using MacPorts so far. I did try `sudo /opt/local/bin/easy_install-2.7 opencv` but I got an error: `Searching for opencv Reading http://pypi.python.org/simple/opencv/ Couldn't find index page for 'opencv' (maybe misspelled?) Scanning index of all packages (this may take a while) Reading http://pypi.python.org/simple/ No local packages or download links found for opencv error: Could not find suitable distribution for Requirement.parse('opencv') ` – jensph Dec 09 '11 at 17:11