Robotics StackExchange | Archived questions

Import error in beginner tutorials

Hello, I am a beginner in ROS and I have been following the tutorial for simple service and client nodes: http://wiki.ros.org/ROS/Tutorials/WritingServiceClient%28python%29 . After following the tutorial and trying to run the program, I get the following error:

Traceback (most recent call last):
  File "/home/klara/catkin_ws/src/beginner_tutorials/scripts/add_two_ints_server.py", line 5, in <module>
    from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse
ImportError: No module named srv

I use Ubuntu 18.04 with ROS melodic, python 2 and catkin. I have sourced the devel/setup.bash so that shouldn't be the problem.

I think the problem is in the service, because everything runs fine when I change
from beginner_tutorials.srv import AddTwoInts,AddTwoIntsResponse to
from rospy_tutorial.srv import AddTwoInts,AddTwoIntsResponse .

I have checked ~/catkin_ws/devel/share/common-lisp/ros/beginner_tutorials/srv/ and it was empty, even though the tutorials say there should be a python script generated there.

I copy pasted the package.xml from rospy_tutorial and changed the package name.

The add_two_ints_server.py is also copy-pasted from the tutorial.

I run catkin_make from catkinws, then source devel/setup.bash, then `rosrun beginnertutorials addtwoints_server.py` and I get the import error.

Thank you in advance for help!

This is my package:

.

├─CMakeLists.txt

├── msg

│   └── Num.msg

├── package.xml

├── scripts

│   ├── add_two_ints_server.py

│   ├── listener.py

│   └── talker.py

└── srv

    └── AddTwoInts.srv

This is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.0.2)

project(beginner_tutorials)

find_package(catkin REQUIRED COMPONENTS
    roscpp
    rospy
    std_msgs
    message_generation
)

add_message_files(
  FILES
  Num.msg
)

add_service_files(
  FILES
  AddTwoInts.srv
 )

generate_messages(
  DEPENDENCIES
   std_msgs  
 )

catkin_package(
  CATKIN_DEPENDS message_runtime 
)

include_directories(
 # ${catkin_INCLUDE_DIRS}
)

catkin_install_python(PROGRAMS
  scripts/talker.py scripts/listener.py scripts/add_two_ints_server.py

 DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

Asked by mayday-sauce on 2021-05-02 09:18:35 UTC

Comments

I copy pasted the package.xml from rospy_tutorial and changed the package name.

Why did you use the package.xml from rospy_tutorial and not from the tutorial you are following ? Maybe you should start again (and properly do the modifications in package.xml and CMakeLists.txt). If it doesn't change anything maybe you could show us the package.xml you are using too. Also, is the file AddTwoInts.srv the same as in the tutorial ?

Asked by Delb on 2021-05-04 02:39:19 UTC

Answers

It looks like the catkin_package setting is not enough. Try the following.

catkin_package(
  CATKIN_DEPENDS message_runtime std_msgs # added std_msgs
)

ref: http://docs.ros.org/en/latest/api/catkin/html/howto/format2/building_msgs.html

Asked by miura on 2021-05-03 08:19:00 UTC

Comments

Thank you, unfortunately, I am still getting the same error.

Asked by mayday-sauce on 2021-05-03 09:01:08 UTC