Ask Your Question

include srv files in python catkin project

asked 2016-11-22 17:52:21 -0600

215 gravatar image

I am currently having some issues with importing my service files into my project. i can't import into my file.. Why is that not possible?

I made it as a rosnode, my src folder of the catkin_workspace looks like this

├── CMakeLists.txt -> /opt/ros/indigo/share/catkin/cmake/toplevel.cmake
└── service
    ├── CMakeLists.txt
    ├── include
    │   └── service
    │       ├──
    │       ├──
    │       └── service_def.pyc
    ├── package.xml
    ├── src
    │   └──
    └── srv
        ├── get.srv
        ├── set.srv
        └── is defined as such

from ..srv import *

And my which i rosrun imports

import service.service_def

So why cant I import my service messages in

error message:

k@k:~/python_service/src$ rosrun service 
Traceback (most recent call last):
  File "/home/k/python_service/src/service/src/", line 5, in <module>
    import service.service_def
  File "/home/k/python_service/src/service/include/service/", line 1, in <module>
    from ..srv import *
ValueError: Attempted relative import beyond toplevel package
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2016-11-23 07:47:17 -0600

jsanch2s gravatar image

updated 2016-11-23 08:57:00 -0600

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 file you can used that package name directly by changing:

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

Here's an MWE for the:


├── CMakeLists.txt
├── package.xml
├── src
│   └── service_node
└── srv
    ├── get.srv
    └── set.srv


cmake_minimum_required(VERSION 2.8.3)

find_package(catkin REQUIRED





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

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





#!/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)
edit flag offensive delete link more


but that doesn't solve the problem.. It doesn't import the srv files?

215 gravatar image 215  ( 2016-11-23 08:21:14 -0600 )edit

@215 Thanks for pointing that out. I've updated my answer.

jsanch2s gravatar image jsanch2s  ( 2016-11-23 08:58:00 -0600 )edit

where are you defining /running the services?

215 gravatar image 215  ( 2016-11-23 09:04:42 -0600 )edit

@215 The services are defined in the srv folder, and are being used in the service_node; x is defined as a set service and y as a get service.

jsanch2s gravatar image jsanch2s  ( 2016-11-23 09:08:59 -0600 )edit

but..? i want to define the how it should set and get things. don't you need a rospy.spin()?

215 gravatar image 215  ( 2016-11-23 09:13:04 -0600 )edit

@215 The code I provided was meant to be a minimal working example. For a tutorial on how to use a server you could check this site. Also is more convenient to define your services in a separate package.

jsanch2s gravatar image jsanch2s  ( 2016-11-23 09:16:17 -0600 )edit

well.. A simple line like this get_service = rospy.Service('_get_service', get, getConfiguration) gives me a error message that it cant find get..

215 gravatar image 215  ( 2016-11-23 09:51:24 -0600 )edit

Try replacing get with service.srv.get.

jsanch2s gravatar image jsanch2s  ( 2016-11-23 10:03:54 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-11-22 17:52:21 -0600

Seen: 795 times

Last updated: Nov 23 '16