Lesson 6: Python Program: custom_service_server.py

I’m trying to get the code running that is given, using the created "my_custom_srv_msg_pkg/MyCustomServiceMessage"

and I can see the message when I run


rossrv list | grep MyCustomServiceMessage

but the issue is when I try making a server as a script and do a rosrun to start the service.

line 5, in <module>
    from my_custom_srv_msg_pkg.srv import MyCustomServiceMessage, MyCustomServiceMessageResponse
ModuleNotFoundError: No module named 'my_custom_srv_msg_pkg'

basically it can’t find the module I created, I made sure to do a catkin_make and resource after creating the message.

This is the copied code, it looks like it is having an issue with finding the service message.

#! /usr/bin/env python

import rospy
from my_custom_srv_msg_pkg.srv import MyCustomServiceMessage, MyCustomServiceMessageResponse # you import the service message python classes
                                                                                         # generated from MyCustomServiceMessage.srv.


def my_callback(request):
    
    print("Request Data==> duration="+str(request.duration))
    my_response = MyCustomServiceMessageResponse()
    if request.duration > 5.0:
        my_response.success = True
    else:
        my_response.success = False
    return  my_response # the service Response class, in this case MyCustomServiceMessageResponse

rospy.init_node('service_client') 
my_service = rospy.Service('/my_service', MyCustomServiceMessage , my_callback) # create the Service called my_service with the defined callback
rospy.spin() # maintain the service open.

Any ideas what I’m doing wrong here? I tried to review the previous created services tutorial, but I don’t see what I’m missing here.

looks like it is a similar issue now that I’ve found it on this question.

I guess catkin_make wasn’t making all the packages?

The following worked to fix this issue of not find the srv package.

catkin_make --only-pkg-with-deps <your_package_name>
source devel/setup.bash

1 Like