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

Revision history [back]

click to hide/show revision 1
initial version

Change from ..srv import * to from service.service_def import *.

Change from ..srv import * to from service.service_def import *.

Change As the error says, you are trying to do a relative import (i.e. using ..). Instead, since you are already setting service as a package in your setup.py file you can used that package name directly by changing:

from ..srv import * in service_def.py to from service.service_def import *.

As the error says, you are trying to do a relative import (i.e. using ..). Instead, since you are already setting service as a package in your setup.py file you can used that package name directly by changing:

from ..srv import * in service_def.py to from service.service_def import *.

Here's an MWE for the:

Structure:

service/
├── CMakeLists.txt
├── package.xml
├── src
│   └── service_node
└── srv
    ├── get.srv
    ├── __init__.py
    └── set.srv

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(service)

find_package(catkin REQUIRED
   rospy
   std_msgs
   message_generation
)

add_service_files(
  FILES
   get.srv
   set.srv
)

generate_messages(
  DEPENDENCIES
  std_msgs
)

catkin_package(
)

package.xml

<?xml version="1.0"?>
<package>
  <name>service</name>
  <version>0.0.0</version>
  <description>The service package</description>

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

  <license>TODO</license>
  <buildtool_depend>catkin</buildtool_depend>

  <build_depend>rospy</build_depend>
  <build_depend>message_generation</build_depend>

  <run_depend>rospy</run_depend>
  <run_depend>message_runtime</run_depend>
</package>

service_node

#!/usr/bin/env python
#-*- encoding: utf-8 -*-

import rospy
import service.srv


def main():
    print("Creating set")
    x = service.srv.set
    print("Creating get")
    y = service.srv.get

if __name__ == '__main__':
    rospy.init_node('service_node', anonymous=True)
    main()