ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ImportError: No module named cv2

asked 2020-10-31 11:08:35 -0500

Dejv311 gravatar image

Hi, I am new to ros. After installing all the necessary things I managed to catkin_make [http://wiki.ros.org/tello_driver]. But after running it with

roslaunch tello_driver tello_node.launch

I get following error. I spent a lot of time trying to fix it, but I am unable to find a solution.

Traceback (most recent call last):
  File "/home/david/catkin_ws/src/tello_driver/nodes/tello_driver_node", line 548, in <module>
    main()
  File "/home/david/catkin_ws/src/tello_driver/nodes/tello_driver_node", line 542, in main
    robot = TelloNode()
  File "/home/david/catkin_ws/src/tello_driver/nodes/tello_driver_node", line 91, in __init__
    self.bridge = CvBridge()
  File "/opt/ros/noetic/lib/python3/dist-packages/cv_bridge/core.py", line 67, in __init__
    import cv2
ImportError: No module named cv2

I have tried:

pip install opencv-python
edit retag flag offensive close merge delete

Comments

Can you import openCV from the python interpreter? In a terminal window, enter "python", then type "import cv2" in the interpreter:

$ python

import cv2

If there is no error, openCV is installed. If there is an error, you may want to try installing without pip:

$ sudo apt install python-opencv

fruitbot gravatar image fruitbot  ( 2020-11-02 14:23:22 -0500 )edit

Find the location of pip in your system by running -

which pip

Let's say, it turns out to be '/usr/bin/pip'. Navigate to '/usr/bin' and then install the required pacake using ther 'pip' command. Let me know if you stil face the same issue.

skpro19 gravatar image skpro19  ( 2020-11-02 15:10:42 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-11-07 08:47:24 -0500

Dejv311 gravatar image

Hello, firstly thanks all of you trying to help, I really do appreciate it! The problem was in bad $PYTHONPATH. You can check current value by typing this into cmd:

echo $PYTHONPATH

Here I show the previous value and the current value. Tutorials on how to change the value of $PYTHONPATH can be easily searched on the internet...

/opt/ros/noetic/lib/python3/dist-packages:/usr/local/lib/python2.7/site-packages /opt/ros/noetic/lib/python3/dist-packages:/usr/local/lib/python3.8/dist-packages

Now, ros will be using python 3 instead of python 2 (Many versions of ros used python 2 by default, but for noetic python 2 is depreciated!!!). Another thing you need to do specifically for the Tello package is to check all the "*.py" files for this specific line (always on top of the file):

#!/usr/bin/env python2

and change it to

#!/usr/bin/env python

This line told the computer to use Python 2 in any case. By deleting the "2" we tell it to use the python specified in the PYTHONPATH. Now go to your workspace and try catkin_make. After you install all required packages to python 3, the program should run no problem. This solution took me about 30 hours to find (i ran into many other problems, because python 2 and python 3 ran at the same time), so I hope it will help you, the reader.

Lastly, let me write here the yaml error that shows up with the python 2/3 collision, so people can find this reply more easily:

Traceback (most recent call last):
  File "/home/david/catkin_ws/src/tello_driver/nodes/tello_driver_node", line 2, in <module>
    import rospy
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/__init__.py", line 47, in <module>
    from std_msgs.msg import Header
  File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/__init__.py", line 1, in <module>
    from ._Bool import *
  File "/opt/ros/noetic/lib/python3/dist-packages/std_msgs/msg/_Bool.py", line 6, in <module>
    import genpy
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/__init__.py", line 34, in <module>
    from . message import Message, SerializationError, DeserializationError, MessageException, struct_I
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 48, in <module>
    import yaml
  File "/usr/lib/python3/dist-packages/yaml/__init__.py", line 400
    class YAMLObject(metaclass=YAMLObjectMetaclass):
                              ^
SyntaxError: invalid syntax
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-10-31 11:08:35 -0500

Seen: 4,566 times

Last updated: Oct 31 '20