Ask Your Question
1

Error while importing services for python : ImportError: No module named srv

asked 2018-06-20 11:50:22 -0600

RobinB gravatar image

updated 2018-06-20 21:01:19 -0600

Hello everyone, I am new to the ROS community and I am currently learning to use ROS kinetic, but I'm unable to find an easy way to create and use a service proprely. I have updated the CMakelist and the package.xml file as described in this tutorials in order to account for the new .srv file, but I still have ImportError. I have also double checked in this Cmakelist documentation. I hope that it is a small and dumb mistake. The weird thing is that the service seems to be recognized by ros. I find that the documentation is pretty vague about this, or maybe I just skipped a step.


Here is the code of my wcomputation_node.py:

#!/usr/bin/env python
import rospy
import numpy as np
from geometry_msgs.msg import Pose2D
from wcomputation.srv import *

Here is the Wservice.srv file

float64 x
float64 y
float64 theta
---
float64 wa
float64 wb
float64 wc

.
├── CMakeLists.txt -> /opt/ros/kinetic/share/catkin/cmake/toplevel.cmake
├── gui
│   ├── CMakeLists.txt
│   ├── launch
│   │   └── gui.launch
│   ├── package.xml
│   └── src
│       └── gui.py
└── wcomputation
    ├── CMakeLists.txt
    ├── msg
    ├── package.xml
    ├── scripts
    │   ├── sendserial.py
    │   ├── wcomputation_node.py
    │   └── wcomputation_node.pyc
    ├── src
    └── srv
        └── Wservice.srv

And finally the service seems to be recognized by ROS

user@ordi:~/Bureau/omniROS/omniROS_ws/src$ rossrv show wcomputation/Wservice float64 x
float64 y
float64 theta
---
float64 wa
float64 wb
float64 wc
user@ordi:~/Bureau/omniROS/omniROS_ws/src$ rossrv info wcomputation/Wservice  
float64 x
float64 y
float64 theta
---
float64 wa
float64 wb
float64 wc

And here is my CMakeList :

EDIT : I do need to use this service via a python script. Here is my CMakeList file :

cmake_minimum_required(VERSION 2.8.3)
project(wcomputation)

## Compile as C++11, supported in ROS Kinetic and newer
# add_compile_options(-std=c++11)

## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
  geometry_msgs
  roscpp
  rospy
  message_generation
  std_msgs
)

## System dependencies are found with CMake's conventions
# find_package(Boost REQUIRED COMPONENTS system)


## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
################################################

## To declare and build messages, services or actions from within this
## package, follow these steps:
## * Let MSG_DEP_SET be the set of packages whose message types you use in
##   your messages/services/actions (e.g. std_msgs, actionlib_msgs, ...).
## * In the file package.xml:
##   * add a build_depend tag for "message_generation"
##   * add a build_depend and a run_depend tag for each package in MSG_DEP_SET
##   * If MSG_DEP_SET isn't empty the following dependency has been pulled in
##     but can be declared for certainty nonetheless:
##     * add a run_depend tag for "message_runtime"
## * In this file (CMakeLists.txt):
##   * add "message_generation" and every package in MSG_DEP_SET to
##     find_package(catkin REQUIRED COMPONENTS ...)
##   * add "message_runtime" and every package in MSG_DEP_SET to
##     catkin_package(CATKIN_DEPENDS ...)
##   * uncomment the add_*_files sections below as needed
##     and list every .msg/.srv/.action file to ...
(more)
edit retag flag offensive close merge delete

Comments

Hi Robin, Have you tried the solution in this post : https://answers.ros.org/question/2716... ?

lmathieu gravatar imagelmathieu ( 2018-06-20 12:33:50 -0600 )edit

Yes I did, I have copied the content of my CMakeList file

RobinB gravatar imageRobinB ( 2018-06-20 21:02:25 -0600 )edit

Where's your python script in this? I don't see any install or python rules

stevemacenski gravatar imagestevemacenski ( 2018-06-21 12:41:55 -0600 )edit

So, maybe you forgot to run catkin_make ? Or did you install anaconda or an other python interpreter that can change your default python 2.7 to python 3 ? btw, you can upload a link to you workspace if you have one, you should have enough Karma (or you can put it in a to show it :) )

lmathieu gravatar imagelmathieu ( 2018-06-21 17:03:43 -0600 )edit

I'm talking about your cmakelists file, there's no install rules for python or setting up the workspace to work with it.

stevemacenski gravatar imagestevemacenski ( 2018-06-21 17:06:59 -0600 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2018-06-20 12:39:51 -0600

Please add your cmakelists file.

I'm going to take a good guess you haven't generated the messages correctly.

add_service_files(DIRECTORY srvs FILES ServiceFile.srv ) generate_messages( DEPENDENCIES std_msgs other_deps )

also add as a catkin dep. message_generation

I forget if you need this in python, but here it is anyhow. add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_generate_messages_cpp)

edit flag offensive delete link more

Comments

Thanks for this, I forgot to add my .srv to add_service_files in CMakeLists.txt! Works perfectly now

latrapo gravatar imagelatrapo ( 2018-12-09 09:00:03 -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

Stats

Asked: 2018-06-20 11:50:22 -0600

Seen: 943 times

Last updated: Jun 20 '18