Search code examples
interfacecommand-line-interfacerosros2

ROS2 throws ModuleNotFoundError when calling 'interface proto' for custom msg


Setup
I use windows and ROS2 Humble and I have created some custom topic messages. ROS2 Galactic and ROS2 Running are also installed and exhibit the same behavior. For the sake of brevity we can focus on ROS2 Humble.

I have created a simple custom message custom/msg/CustomMessage. Defined as follows:

string custom_string
trajectory_msgs/JointTrajectory custom_trajectory

Behavior with existing messages
Using the command: ros2 interface proto trajectory_msgs/msg/JointTrajectory returns:

"header:
  stamp:
    sec: 0
    nanosec: 0
  frame_id: ''
joint_names: []
points: []
"

The trajectory_msgs/msg/JointTrajectory interface appears in the list provided by the command: ros2 interface list.

Behavior with custom messages
Using this command for the custom message: ros2 interface proto custom/msg/CustomMessage returns:

Traceback (most recent call last):
  File "c:\dev\ros2humble\Scripts\ros2-script.py", line 33, in <module>
    sys.exit(load_entry_point('ros2cli==0.18.4', 'console_scripts', 'ros2')())
  File "c:\dev\ros2humble\Lib\site-packages\ros2cli\cli.py", line 89, in main
    rc = extension.main(parser=parser, args=args)
  File "c:\dev\ros2humble\Lib\site-packages\ros2interface\command\interface.py", line 35, in main
    return extension.main(args=args)
  File "c:\dev\ros2humble\Lib\site-packages\ros2interface\verb\proto.py", line 33, in main
    yaml = interface_to_yaml(args.type)
  File "c:\dev\ros2humble\Lib\site-packages\ros2interface\api\__init__.py", line 46, in interface_to_yaml
    interface = utilities.get_interface(identifier)
  File "c:\dev\ros2humble\Lib\site-packages\rosidl_runtime_py\utilities.py", line 23, in get_interface
    return import_message_from_namespaced_type(get_namespaced_type(identifier))
  File "c:\dev\ros2humble\Lib\site-packages\rosidl_runtime_py\import_message.py", line 30, in import_message_from_namespaced_type
    module = importlib.import_module(
  File "C:\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'custom'

The custom/msg/CustomMessage interface appears in the list provided by the command: ros2 interface list
Calling the command: ros2 interface show custom/msg/CustomMessage gives the expected response:

string custom_string
trajectory_msgs/JointTrajectory custom_trajectory
        std_msgs/Header header
                builtin_interfaces/Time stamp
                        int32 sec
                        uint32 nanosec
                string frame_id
        string[] joint_names
        JointTrajectoryPoint[] points
                float64[] positions
                float64[] velocities
                float64[] accelerations
                float64[] effort
                builtin_interfaces/Duration time_from_start
                        int32 sec
                        uint32 nanosec

Question - Problems
I would expect the command ros2 interface proto to create a prototype for the custom message. However, this does not happen and the output is a ModuleNotFoundError. I tried different messages, different package names but the command always ends up with the same error.


Solution

  • It was finally possible to use the CustomMessage just like the built-int messages. The error appeared while using the following command to build only the custom project.

    colcon build --merge-install --packages-select custom
    

    replacing this commmand with

    colcon build --merge-install 
    

    solves the issue and the ros2 CLI can be used normally.

    In other words, building the complete workspace seems to solve all issues, allowing ros2 commands to be used normally. While partially building the workspace does not.