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
Was having the exact same problem. The issue is that ROS creates it's own cv2.so file for python 2, and then routes every import request to that file. It's a pretty easy fix:
go to your site-packages folder
note, if you are using a virtual environment, you must be within that, and should instead do something like:
Then, force a new sym-link this time using the -f flag
And that should fix things!
Similar problem over here. As others suggested,
/opt/ros/kinetic/setup.bash
appends a path to ROS opencv in thePYTHONPATH
variable.If you are working with multiple
virtualenv
s and you need a solution that will work in most of the cases, then you can put the following code snippet in your.bashrc
:So the idea is that if you have a centralised directory of all of your
virtualenv
s (e.g when you usevirtualenvwrapper
) we can search for those directories using:given that all of our
virtualenv
s are under~/.virtualenvs
. This should give us a list of all of ourvirtualenv
s root directory.We are then looping over the array of virtualenv directories and we are appending their path (e.g
~/.virtualenvs/testenv/lib/python2.7/site-packages
) to thePYTHONPATH
. Note that this should be done just after thesource /opt/ros/kinetic/setup.bash
.It's not a perfect solution to the problem as you can still get conflicts if two envs have different opencv versions, but for the initial problem, at least it should work.
Alternatively, you can just manually do the same trick for the desired virtualenv:
If none of those solutions works for you (as in my case) you could still try to trick your system into importing the right opencv
Maybe you might consider replacing the ros python path at the right location after importing cv2.
It seems that my python had problems importing the correct cv2 even though the path was set correctly, probably because of the weird naming of the python3 cv2 library (cv2.cpython-35m-x86_64-linux-gnu.so) compared to the cv2.so I have in /opt/ros/kinetic/lib/python2.7/dist-packages
Step1: Find the path where your cv2.so is installed (if you use the python-cv wheel to install the opencv)
Step2: Export the path to your .bashrc or .zshrc file, just like this:
Thanks to @lxrd-aj
It looks like this problem is caused by ROS adding
/opt/ros/kinetic/lib/python2.7/dist-packages
to the python path. This actually happens when you activate ROS with the commandsource /opt/ros/kinetic/setup.bash
. This line is often added at the end of your bashrc file, in/home/username/.bashrc
.A workaround is to remove this line from the bashrc file. This way the python3 opencv packages will be correctly used, and you can still run
source /opt/ros/kinetic/setup.bash
to use ROS. However, this does mean you cannot use ROS and python3 from the same environment.Hopefully someone can come up with a better answer, but this should work until then.
If you are working with anaconda, activate the environment you want to work from, and remove the culprit from
sys.path
.To do so, open a python3 console, from which:
You will see several path, among which you should notice:
Then remove it:
Tested with python3.5 on anaconda3 with locally compiled opencv. This is likely applicable to
virtualenvs
as well.For a permanent solution, remove the path
'/opt/ros/kinetic/lib/python2.7/dist-packages'
from~/.bashrc
as mentioned in @Paul's answer.