After install ROS Kinetic, cannot import OpenCV

Solution 1:

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 command source /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.

Solution 2:

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:

>>> import sys
>>> print(sys.path)

You will see several path, among which you should notice:

'/opt/ros/kinetic/lib/python2.7/dist-packages'

Then remove it:

>>> sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')

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.

Solution 3:

As pointed out, the source /opt/ros/kinetic/setup.bash command in your .bashrc modifies the PYTHONPATH to be:

> echo $PYTHONPATH
/opt/ros/kinetic/lib/python2.7/dist-packages

In my case, since I am using a virtualenv for my Python 3 projects, I just ran the following command to clear the PYTHONPATH variable, while the virtualenv is activated.

unset PYTHONPATH

Now, importing cv2 in Python 3 virtualenv works cleanly. I verified the path of cv2:

In [1]: import cv2
In [2]: cv2.__file__
Out[2]: '<path_to_virtualenv>/lib/python3.5/site-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so'

To avoid having to run this command every time I activate that virtualenv, I added it to the /bin/activate file in the virtualenv directory, as follows:

...
# unset irrelevant variables
deactivate nondestructive

unset PYTHONPATH

VIRTUAL_ENV="/home/kaiyuzh/pyenv/py3"
export VIRTUAL_ENV
...

Solution 4:

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

cd /usr/local/lib/python3.5/site-packages/

note, if you are using a virtual environment, you must be within that, and should instead do something like:

cd ~/.virtualenvs/cv/lib/python3.5/site-packages/

Then, force a new sym-link this time using the -f flag

ln -sf /usr/local/lib/python3.5/site-packages/cv2.so cv2.so

And that should fix things!

Solution 5:

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

ros_path = '/opt/ros/kinetic/lib/python2.7/dist-packages'

if ros_path in sys.path:

    sys.path.remove()

import cv2

sys.path.append('/opt/ros/kinetic/lib/python2.7/dist-packages')

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