4

I am having some difficulties installing opencv with python 3.5.

I have linked the cv files, but upon import cv2 I get an error saying ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2.so, 2): Symbol not found: _PyCObject_Type or more specifically:

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/Jamie/Desktop/tester/test.py Traceback (most recent call last): File "/Users/Jamie/Desktop/tester/test.py", line 2, in import cv File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv.py", line 1, in from cv2.cv import * ImportError:dlopen(/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2.so, 2): Symbol not found: _PyCObject_Type Referenced from: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2.so Expected in: flat namespace in /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/cv2.so

I have linked cv.py and cv2.so from location /usr/local/Cellar/opencv/2.4.12_2/lib/python2.7/site-packages correctly into /Library/Frameworks/Python.framework/Versions/3.5/bin

Would anybody be able to help please?

Thanks very much

1
  • 1
    Are you trying to load an opencv compiled for Python 2 using Python 3.5 ? It won't work. I documented here how I compiled opencv3 for Python 3 a few month ago, if it helps: github.com/julienpalard/grid-finder Commented May 31, 2016 at 22:26

3 Answers 3

6

No need to change the python version, you can just use the pip command open cmd ( admin mode) and type

pip install opencv-python

Sign up to request clarification or add additional context in comments.

Comments

2

Found an answer - follow the instructions on this website BUT you have to change to the version of python you are using.

Also, I didn't bother with the virtual environments.

And lastly cv2.so is actually called cv2.cpython-35m-darwin.so in the build/lib folder that you make.

Then it works.

3 Comments

For the future, you could also just create a symlink with ln -s cv2.cpython-35m-darwin.so cv2.so so you can import it using import cv2. :-)
Hey gglasses, how do I create a symlink as you said?
ln -s cv2.cpython-35m-darwin.so cv2.so
0

Great answer JamieS, I also followed that website. I tried to make the whole process repeatable with these make targets..

opencv-build:
    source $(VENV_DIR)/bin/activate && \
    cd $(OPENCV_SRC) && \
    mkdir -p build && \
    cd build && \
    cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=$(realpath $(BUILD_DIR)) \
    -D PYTHON3_NUMPY_INCLUDE_DIRS=$(realpath $(VENV_DIR)/lib/python3.5/site-packages/numpy/core/include) \
    -D BUILD_opencv_python3=ON \
    -D INSTALL_C_EXAMPLES=OFF \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=$(realpath $(OPENCV_CONTRIB_SRC)/modules) \
    -D BUILD_EXAMPLES=ON ..  && \
    make -j4 -C . 

To make it appear in python3 virtual env however you need to do this then:

so-copy:
    cp $(realpath $(OPENCV_SRC)/build/lib/cv2.so) $(realpath $(VENV_DIR)/lib/python3.5/site-packages/)
    cp $(realpath $(OPENCV_SRC)/build/lib/python3/cv2.cpython-35m-darwin.so) $(realpath $(VENV_DIR)/lib/python3.5/site-packages/)

Then you can test if it works..

import cv2  # Imports without problems...

I think for osx the cv2.cpython-35m-darwin.so is the only one you need, but I just copied both.

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.