After install ROS Kinetic, cannot import OpenCV

2019-01-17 20:58发布

I have first installed openCV from source using this script. When I tested it was working well.

After I installed ROS kinetic, and open python3 and run import cv2, got the following error:

Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type

9条回答
Ridiculous、
2楼-- · 2019-01-17 21:18

I tried to remove /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so. Now It is working.

查看更多
一夜七次
3楼-- · 2019-01-17 21:29

Actually, I also encounter this problem. I just commented-out the source:

/opt/ros/kinetic/setup.bash

and then, open the terminal, In the anaconda lib path I executed:

sudo ln -sf /home/apg/miniconda3/lib/python3.6/site-packages/cv2.so /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so

/home/apg/miniconda3 is the path where my python3 was installed, and /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so was the path that opencv-python was installed.

I don't know yet why this command works, but it did work for me to solve the problem that I was not able to import cv2 previously.

查看更多
一纸荒年 Trace。
4楼-- · 2019-01-17 21:35

During ROS setup running /opt/ros/kinetic/setup.bash, the script between other things will go to every distribution package for python2 and python3 and it will add it to the python system path. When you try to import cv2 using python3 it will first find the cv2 binding for Python2 as included in the path.

If you are not planning on using python2.7 at all for your ROS project, consider removing entirely the python2.7 OpenCV package, so it won't be added to the path when running the ros setup script:

$ rm /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so

Another more gentle approach if you are planning to use both Python versions would be to create two virtual environments with the correct bindings, and activate one or another depending on your requirements.

查看更多
登录 后发表回答