ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
6

Tutorial 1.16 "ImportError: No module named beginner_tutorials.srv" with Catkin system build

asked 2014-01-04 03:15:46 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >75 is welcome to improve it.

Hi, I started ROS tutorials, using catkin system as a workspace builder. I didn't had any issue until this tutorial examining service and client nodes.

After creating having written my service and client nodes in the previous tutorial, I tried to run them :

$ rosrun beginner_tutorials add_two_ints_server.py

and i get this error :

"Traceback (most recent call last):
File "~/catkin_ws/src/beginner_tutorials/scripts/add_two_ints_server.py", line 4, in <module> from beginner_tutorials.srv import *

ImportError: No module named beginner_tutorials.srv"

I searched how to resolve this problem, but i didn't find an answer for catkin system build (for rosbuild, it seems like uncomment #rosbuild_genmsg() and #rosbuild_gensrv() lines on the CMakeLists.txt is enough).

However, there is no comments like that in my CMakeLists.txt (due to the fact I used catkin and not rosbuild). I even tried to rebuild my workspace by erasing devel and build files in my catkin workspace, and rebuild it, but it didn't work.

I could use some help on this if you know how to do it. Thank you !

edit retag flag offensive close merge delete

4 Answers

Sort by » oldest newest most voted
6

answered 2014-01-10 00:56:29 -0500

Wolf gravatar image

updated 2014-03-25 04:27:50 -0500

As pointed out in tutorial CreatingMsgAndSrv for message/service generation with catkin you need to:

  • Add to package.xml:

    <build_depend>message_generation</build_depend>
    <run_depend>message_runtime</run_depend>

  • Add message_generation in CMakeLists.txt as REQUIRED catkin component (like any package, e. g. roscpp):

    find_package( catkin REQUIRED COMPONENTS roscpp message_generation )

  • uncomment add_message_files in CMakelists.txt and add your msg files with messages to be generated:

    add_message_files( FILES my_message.msg )

  • uncomment add_service_files in CMakelists.txt and add your srv files with services to be generated:

    add_service_files( FILES beginner_tutorials.srv )

  • uncomment generate_messages in CMakeLists.txt with packages containing msgs depending on:

    generate_messages( DEPENDENCIES std_msgs )

  • in CMakeLists.txt add ${PROJECT_NAME}_generate_messages_cpp as dependency of your target that required the message to be generated before being compiled (in C++, I am not sure if is also necessary for python)

    add_executable( foo_exec src/foo.cpp)
    target_link_libraries( foo_exec ${catkin_LIBRARIES} )
    add_dependencies( foo_exec ${PROJECT_NAME}_generate_messages_cpp )

edit flag offensive delete link more

Comments

2

Isn't the service called AddTwoInts.srv and not beginner_tutorials.srv ? http://wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv

TFinleyosu gravatar image TFinleyosu  ( 2014-03-19 17:35:32 -0500 )edit

Yes, this is want mentioned tutorial says. Here a little abstracted necessary steps for message generation of any message with catkin

Wolf gravatar image Wolf  ( 2014-03-19 21:18:01 -0500 )edit

My issue was caused by jumping ahead in the tutorials. I had done the tutorials a while back and this time I did not take the time to go back through them all to build the CMakeList properly. Wolf, can you check your *add_message_files( FILES beginner_tutorials.srv ) I think it's add_service_files

TFinleyosu gravatar image TFinleyosu  ( 2014-03-25 04:11:27 -0500 )edit

Oops, yes right, I changed. Thank you =)

Wolf gravatar image Wolf  ( 2014-03-25 04:28:31 -0500 )edit

I just got this same problem using rosbuild, and i had to change from beginner_tutorials.srv import * in the client.py to "from rospy_tutorials.srv import *. Am i sure this is correct? Not really, but everything else i tried failed.

ros_geller gravatar image ros_geller  ( 2015-08-25 22:53:22 -0500 )edit

DEpends on how your package is named, if it's name is rospy_tutorials you are correct

Wolf gravatar image Wolf  ( 2015-09-03 01:59:18 -0500 )edit

Hi I know its old but I would also add:

catkin_package( CATKIN_DEPENDS roscpp rospy std_msgs message_runtime )

It feels like it only works for me if I uncomment the line in the catkin_package section and add message runtime to it.

Bant gravatar image Bant  ( 2016-08-02 11:26:07 -0500 )edit

Note: For package.xml files in XML Format 2, run_depend should instead be exec_depend, as pointed out Here.

IsakEngström gravatar image IsakEngström  ( 2020-08-27 10:43:09 -0500 )edit
20

answered 2016-09-05 16:53:42 -0500

dheater gravatar image

Wasn't apparent to me at first. After the catkin_make you have to source the devel/setup.bash file again, even if you've done so before in that terminal.

edit flag offensive delete link more

Comments

This worked for me. I had already done everything described by @Wolf, but forgot this step.

sourcecreator gravatar image sourcecreator  ( 2017-04-22 08:17:16 -0500 )edit
1

answered 2017-11-06 15:23:25 -0500

Ahiska gravatar image

run this command source ~/catkin_ws/devel/setup.bash because your system needs to see the changes you make. ( for example,After the "catkin_make or catkin_make install" command )

edit flag offensive delete link more
0

answered 2016-05-23 01:05:37 -0500

rpxz gravatar image

its saying "import:command not found"

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2014-01-04 03:15:46 -0500

Seen: 16,771 times

Last updated: Mar 25 '14