Example 6.8: ModuleNotFoundError: No module named 'my_custom_srv_msg_pkg'

Hello,
I am having problems running Example 6.8 for ROS Basics in 5 Days (Python).
When attempting rosrun my_custom_srv_msg_pkg custom_service_server.py, the following error occurs:

Traceback (most recent call last):
File “/home/user/catkin_ws/src/my_custom_srv_msg_pkg/scripts/custom_service_server.py”, line 4, in
from my_custom_srv_msg_pkg.srv import MyCustomServiceMessage, MyCustomServiceMessageResponse
ModuleNotFoundError: No module named ‘my_custom_srv_msg_pkg’

The steps for Exercise 6.8 have been followed as shown in the tutorial, described below.

In Shell 1:

cd ~/catkin_ws/
catkin_create_pkg my_custom_srv_msg_pkg rospy
cd ~/catkin_ws/src/my_custom_srv_msg_pkg/
mkdir srv
cd srv
touch MyCustomServiceMessage.srv

MyCustomServiceMessage.srv contains:

int32 duration    # The time (in seconds) during which BB-8 will keep moving in circles
---
bool success      # Did it achieve it?

The CMakeLists.txt of package my_custom_srv_msg_pkg contains (copied and pasted from the tutorial):

cmake_minimum_required(VERSION 2.8.3)
project(my_custom_srv_msg_pkg)


## Here is where all the packages needed to COMPILE the messages of topics, services and actions go.
## It's only getting its paths, and not really importing them to be used in the compilation.
## It's only for further functions in CMakeLists.txt to be able to find those packages.
## In package.xml you have to state them as build
find_package(catkin REQUIRED COMPONENTS
  std_msgs
  message_generation
)

## Generate services in the 'srv' folder
## In this function will be all the action messages of this package ( in the action folder ) to be compiled.
## You can state that it gets all the actions inside the action directory: DIRECTORY action
## Or just the action messages stated explicitly: FILES my_custom_action.action
## In your case you only need to do one of two things, as you wish.
add_service_files(
  FILES
  MyCustomServiceMessage.srv
)

## Here is where the packages needed for the action messages compilation are imported.
generate_messages(
  DEPENDENCIES
  std_msgs
)

## State here all the packages that will be needed by someone that executes something from your package.
## All the packages stated here must be in the package.xml as exec_depend
catkin_package(
  CATKIN_DEPENDS rospy
)


include_directories(
  ${catkin_INCLUDE_DIRS}
)

The package.xml of package my_custom_srv_msg_pkg contains (copied and pasted from the tutorial):

<?xml version="1.0"?>
<package format="2">
  <name>my_custom_srv_msg_pkg</name>
  <version>0.0.0</version>
  <description>The my_custom_srv_msg_pkg package</description>

  <maintainer email="user@todo.todo">user</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>message_generation</build_depend>
  <build_export_depend>rospy</build_export_depend>
  <exec_depend>rospy</exec_depend>
  <build_export_depend>std_msgs</build_export_depend>
  <exec_depend>std_msgs</exec_depend>
  <build_export_depend>message_runtime</build_export_depend>
  <exec_depend>message_runtime</exec_depend>

  <export>
  </export>
</package>

In Shell 1:

cd ~/catkin_ws
catkin_make
source devel/setup.bash

The tutorial states that the shell should display “Generating Python code from SRV my_custom_srv_msg_pkg/MyCustomServiceMessage”. This does NOT occur. However, when running rossrv list | grep MyCustomServiceMessage, the service message created does show as my_custom_srv_msg_pkg/MyCustomServiceMessage

Then, to create the service server:
In Shell 1:

cd ~/catkin_ws/src/my_custom_srv_msg_pkg/
mkdir scripts
cd scripts
touch custom_service_server.py
chmod +x custom_service_server.py

custom_service_server.py contains (copied from the tutorial):

#! /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.

After saving, in Shell 1:

cd ~/catkin_ws
catkin_make
source devel/setup.bash
rosrun my_custom_srv_msg_pkg custom_service_server.py

This causes the error as below:

ModuleNotFoundError: No module named ‘my_custom_srv_msg_pkg’

Any assistance with this would be appreciated, as no changes have been made from the tutorial steps, other than the method I used to create the MyCustomServiceMessage.srv file using touch rather than vim. I have also tried to run the custom_service_server.py from a package other than my_custom_srv_msg_pkg, but the same error occurs.

Other forum posts have been closed and flagged as SOLVED after the user was told to run source devel/setup.bash before running the script, but I can assure that this has been done beforehand and the error persists.

Hi @Pegolas, welcome to the community!

So, your issue is that your server node does not find your custom message package.

  • Does roscd my_custom_srv_msg_pkg take you to the package’s location in your workspace? this would mean that it compiled correctly and the system can find it. If it doesn’t you can try compiling only the package with: catkin_make --only-pkg-with-deps <target_package> and source the workspace again when it’s done.
  • Does the exercise instruct you to create custom_service_server.py in my_custom_srv_msg_pkg or in another package?
1 Like

Hi @roalgoal,
Thank you for such a quick response.

Compiling the package using catkin_make --only-pkg-with-deps my_custom_srv_msg_pkg has solved it, and the server node is now able to run without the error.

Thank you!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.