python-3.ximportroscatkinrospy

how to import ROS .msg files in python which are not part of the usual .msg path?


I am using this driver here for ROS. The driver is inside my ROS catkin workspace (catkin_ws). Inside this workspace I have another package from where I want to import TelloStatus which is located in tello_driver/msg/TelloStatus.msg

Usually messages (msg) in ROS are imported this way:

Examples:

from geometry_msgs.msg import Twist
from std_msgs.msg import Empty
from sensor_msgs.msg import Imu

I think that these files are located at the following path on the system:

opt/ros/noetic/share

So my question is how can I import TelloStatus which is not part of this path but part of the tello_driver package?

I tried to import it the following way:

import sys
sys.path.append('/home/usr/catkin_ws/src/tello_driver/msg/TelloStatus.msg')
import TelloStatus

but then I get this error message:

import TelloStatus
ModuleNotFoundError: No module named 'TelloStatus'

Thanks for help in advance!


Solution

  • You would still import it the same way as any other message: from tello_driver.msg import TelloStatus.msg. The important part is you must have the correct environment sourced; this is the purpose of source <install_dir/setup.bash>. Make sure you source whatever install directory the build files are in. As a note, you should never try to add files in src/ to be used at runtime this is what install or devel is for.

    Editing based on your comment:

    There's a few things to make sure happen here. First in the package tello_driver make sure the message files are actually set to build in CMakeLists.txt. Make sure it includes lines to add message files

    add_message_files(
      FILES
      TelloStatus.msg
    )
    

    then make sure messages are being built

    generate_messages(
      DEPENDENCIES
      std_msgs
    )
    

    Also make sure the package.xml has the dependencies to build messages by adding(if it's not already there)

    <build_depend>message_generation</build_depend>
    <exec_depend>message_runtime</exec_depend>
    

    Finally before building again make sure to clean your workspace with catkin_make clean then rebuild with catkin_make. After doing that and sourcing devel/setup.bash you can verify the message exists via rosmsg show TelloStatus