13

HI! I'm trying to install opencv and use it with python, but when I compile it I get no errors but I can't import cv module from python:

patrick:release patrick$ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named cv

The code I used to compile it is this:

cd opencv
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON
make
sudo make install

how can I get it working with python?

4
  • I posted a blog post showing how to install OpenCV 2.1 on Snow Leopard. Maybe you can check it out. Commented Jul 25, 2010 at 19:05
  • thanks but it doesn't work for me :( Commented Jul 26, 2010 at 10:23
  • Are you getting the same error? Commented Jul 26, 2010 at 13:39
  • stackoverflow.com/questions/19891790/… Commented Jul 29, 2014 at 17:51

8 Answers 8

18
brew tap homebrew/science
brew install opencv
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
Sign up to request clarification or add additional context in comments.

1 Comment

I think it would be more helpful for the OP and further visitos when you add some explaination to your intension.
4

I found here a way to install opencv for python: http://recursive-design.com/blog/2010/12/14/face-detection-with-osx-and-python/ :)

1 Comment

It works the same also for Linux, not only OSX. The important step is to define PYTHONPATH environment variable according to where cv.so is installed.
2

We can install opencv for Python for Mac OS X with home-brew.

First, install home-brew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

You can see the details for how to install homebrew. http://brew.sh

If you don't install Python, install python(brew will install python2.7):

brew install python

Maybe you want install Python3: brew install python3

Then install opencv3 for Python3:

brew install opencv3 --with-python3

If you want install opencv3 for Python(Python2.7): brew install opencv3 --with-python

OR install opencv2 for Python3: brew install opencv --with-python3

OR if you want install opencv2 for Python3: brew install opencv --with-python3

Finally, maybe you will link site-packages of opencv to the site-packages of Python.

Notes: In the follow command, /usr/local/opt/opencv3/lib/python3.5/site-packages is the directory of opencv3's site-packages, /usr/local/lib/python3.5/site-packages/ is the directory of Python3.5's site-packages.

Maybe you should change the two to your own OPENCV AND PYTHON site-packages directories.

echo /usr/local/opt/opencv3/lib/python3.5/site-packages >> /usr/local/lib/python3.5/site-packages/opencv3.pth

Comments

2
easy_install pip
pip install opencv-python --user
export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH

Maybe you will use sudo,

and the path of installed may not be like mentioned.

Comments

1

This worked for me (change python36 to whatever version you want)

sudo port install opencv +avx2 +python36 +qt5 +contrib +eigen

I got this error and had to apply the patch there (download link)

Apply the patch with:

sudo patch /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/opencv-3.2.0/modules/highgui/src/window_QT.cpp ~/Downloads/patch-cpp11-narrowing-error.diff

Then run sudo port install -N opencv +avx2 +python36 +qt5 +contrib +eigen again

Comments

0

You could try ctypes-opencv -- not sure why building and installing with -D BUILD_PYTHON_SUPPORT=ON didn't work for you (maybe it doesn't know where to install the Python wrappers in osx...?), but the ctypes wrappers should, in theory, work anyway.

1 Comment

the build_python instruction seems to be not working because it doesn't create the cv.pyd (it creates only cv.so). Anyway I just tried ctypes-opencv and I get this error: dpaste.com/221536
0

If you have you want a simple and quick install in Windows, you can download Python(x,y). This distribution includes OpenCv. Be sure to specify that you want to install OpenCV in the installation setup, because it is not installed by default.

2 Comments

Thanks, but I'm on os x currently :)
@insys I fixed the link.
0

When using Virtual Environment

Thanks to @user495470. Follow these steps

brew update
brew install -v cmake 
brew install opencv`

If Part 1 didn't work kindly follow Part 2

Part I
Next step might work sometime, although it didn't work for me
export PYTHONPATH="/VENV_PATH/python2.7/site-packages:$PYTHONPATH"
Then check in python IDE check with import cv or import cv2

Part 2
Go to this path /usr/local/Cellar/opencv/3.4.3/lib/python2.7/site-packages/ or /usr/local/lib/python2.7/site-packages
Copy cv2.so file
Paste it /VENV_PATH/lib/python2.7/site-packages here
Then check in python IDE check with import cv or import cv2

Kindly let me know if this thing works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.