ROS Python, how to import source library

asked 2022-06-09 09:50:11 -0500

Ormuzd gravatar image

Hi Everyone,

My env: Ubuntu 18.04, VS Code IDE, ROS Melodic.

I want to have a node, which can record the frame by RGB-D camera D455 and publish the color and depth frames. For this task, I have installed pyrealsense2 and opencv4 libraries and corrected the PYTHONPATH variable in bashrc file. When I run the next script by VScode:

from darknet_ros_msgs.msg import BoundingBoxes
import cv2                                # state of the art computer vision algorithms library
import numpy as np                        # fundamental package for scientific computing
import time
print(cv2.__version__)
import pyrealsense2 as rs                 # Intel RealSense cross-platform open-source API
print("Environment Ready")

I received the next output:

4.4.0
Environment Ready

However, when I tried to use

rosrun package CV_node

with the next code:

#!/usr/bin/env python
import rospy
from darknet_ros_msgs.msg import BoundingBoxes
import cv2                                # state of the art computer vision algorithms library
import numpy as np                        # fundamental package for scientific computing
import time
print(cv2.__version__)
import pyrealsense2 as rs                 # Intel RealSense cross-platform open-source API
print("Environment Ready")

I have the error:

4.1.1
Traceback (most recent call last):
  File "/home/nvidia/catkin_ws/src/package/scripts/CV_node.py", line 8, in <module>
    import pyrealsense2 as rs                 # Intel RealSense cross-platform open-source API
ImportError: No module named pyrealsense2

As you can see I can't import Realsense library and also I have the different versions of OpenCV libraries (version 4.4 is the version of the source library).

To add the path to the OpenCV4.4 and Realsense libraries I added in bashrc file the next strings:

PYTHONPATH="/usr/local/lib/python3.6/pyrealsense2:/usr/local/lib/python3.6/dist-packages/cv2/python-3.6$PYTHONPATH"
export PYTHONPATH

Could you help me, how to use the path from bashrc file for rosrun command? Thank you in advance!

edit retag flag offensive close merge delete