I get an import error when I try to import opencv in python.

Asked 2 years ago, Updated 2 years ago, 109 views

I installed opencv2 on brew, but when I try to import on python, I get the following error.

By the way, my environment is as follows:
python version: 3.6.0
Mac OS version: 10.12.3

File"<stdin>", line 1, in<module>ImportError:
dlopen(/Users/togokida/.pyenv/versions/3.6.0/lib/python 3.6/site-packages/cv2.so,
2)—Symbol not found:_PyCObject_Type Referenced from:
/Users/togokida/.pyenv/versions/3.6.0/lib/python 3.6/site-packages/cv2.so
Expected in —flat namespace in
/Users/togokida/.pyenv/versions/3.6.0/lib/python 3.6/site-packages/cv2.so

I wonder if the opencv I installed doesn't link well with python, so I looked at some blogs on the Internet, but it didn't work...I would appreciate your help.

python opencv

2022-09-30 21:20

1 Answers

I don't know what to do with a Mac, but at least OpenCV should be linked to Python.
There seems to be an article like this.I didn't build it on my Mac, so I don't know if this case fits, but I think it will be helpful.

  • http://qiita.com/shim0mura/items/b0ec437206ed3d19d878

    At some point, the Python 2.7 module was loaded.Therefore, when I built OpenCV in brew, the so file was placed in /usr/local/opt/opencv3/lib/python 2.7/site-packages/, and because of the build on python 2.7 premise, I got the following error and couldn't read it in python:

  • http://qiita.com/xaskg/items/690ce9048e708de41166

    If you combine the path you looked at with the python-related warning that came out of the brain doctor, you can see the following:

    • The path configured from site.py where Python is calling → OK
    • Path set from homebrew (under $HOME/.pyenv) → Abnormal

    Probably calling site-packages with 2.x path configured from homebrew.
    The path you see in homebrew is written in the file homebrew.pth in $HOME/.local/lib/python3.5/site-packages.(And site.py is probably loading this homebrew.pth.Maybe.)

    If you open the inside...

    homebrew.pth
    import site; site.additedir("/usr/local/lib/python 2.7/site-packages")

    Bingo!!!
    As expected, this is it!!!
    If you comment out or delete the contents...
    Python is back!!!

http://qiita.com/shim0mura/items/b0ec437206ed3d19d878

At some point, the Python 2.7 module was loaded.Therefore, when I built OpenCV in brew, the so file was placed in /usr/local/opt/opencv3/lib/python 2.7/site-packages/, and because of the build on python 2.7 premise, I got the following error and couldn't read it in python:

http://qiita.com/xaskg/items/690ce9048e708de41166

If you combine the path you looked at with the python-related warning that came out of the brain doctor, you can see the following:

  • The path configured from site.py where Python is calling → OK
  • Path set from homebrew (under $HOME/.pyenv) → Abnormal

Probably calling site-packages with 2.x path configured from homebrew.
The path you see in homebrew is written in the file homebrew.pth in $HOME/.local/lib/python3.5/site-packages.(And site.py is probably loading this homebrew.pth.Maybe.)

If you open the inside...

homebrew.pth
import site; site.additedir("/usr/local/lib/python 2.7/site-packages")

Bingo!!!
As expected, this is it!!!
If you comment out or delete the contents...
Python is back!!!

By the way, in my case (OS is CentOS 6.6), I built Python in shared library mode (./configure --enable-shared) so that OpenCV uses this Python.To do this, we set the path of Python's so file in ld.so.conf and ldconfig.
I think it's one of the ways to handle it on a virtual OS using a virtual machine instead of working hard on a Mac.


2022-09-30 21:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.