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

Importing services declared in the same package in python and catkin

asked 2014-05-13 23:18:59 -0600

IvanV gravatar image

updated 2014-06-02 08:03:11 -0600

I'm building om groovy a catkin package that declares a service and contains a python node that acts as server for the service.

The service is declared in the file usv_comm/srv/sendTwist.srv.

The node script is in the file usv_comm/scripts/usv_comm.py.

The service is listed correctly when I make a rossrv list and rossrv show usv_comm/sendTwist.

The service is imported into the python script by from usv_comm.srv import sendTwist.

However, when I run the node as rosrun usv_comm usv_comm.py, I get the following error:

from usv_comm.srv import sendTwist
ImportError: No module named srv

I have previously declared and used services with rosbuild without problems, but I'm quite new to catkin so I'm basically lost right now.

I have followed the tutorials for creating srv (wiki.ros.org/ROS/Tutorials/CreatingMsgAndSrv) and using services (wiki.ros.org/ROS/Tutorials/WritingServiceClient%28python%29) for catkin and python.

Any insight in this problem will be appreciated.

Configuration files:

setup.py

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

d = generate_distutils_setup(
    packages=['usv_comm'],
    package_dir={'': 'scripts'}
)

setup(**d)

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(usv_comm)

find_package(catkin REQUIRED COMPONENTS
  rospy
  roscpp
  std_msgs
  geometry_msgs
  message_generation
)

add_service_files(
  DIRECTORY srv
  FILES
  sendTwist.srv
)

generate_messages(
  DEPENDENCIES
  std_msgs
  geometry_msgs
)

catkin_package(
  CATKIN_DEPENDS rospy std_msgs geometry_msgs message_runtime
)

include_directories(
  ${catkin_INCLUDE_DIRS}
)

package.xml

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

  <build_depend>message_generation</build_depend> 
  <run_depend>message_runtime</run_depend> 
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>rospy</build_depend>
  <build_depend>roscpp</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>geometry_msgs</build_depend>
  <run_depend>rospy</run_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>std_msgs</run_depend>
  <run_depend>geometry_msgs</run_depend>

</package>

Thank you and best regards.

Edit 1: Added CMakeLists.txt file.

Edit 2: Added package.xml

Edit 3: Added setup.py

edit retag flag offensive close merge delete

Comments

Did your package build successfully? What is in your ~/catkin_ws/devel/lib/python2.7/dist-packages/usv_comm/srv folder?

joq gravatar image joq  ( 2014-05-14 04:21:10 -0600 )edit

Yes, the package build successfully. In that folder there are two files: __init__.py and _sendTwist.py.

IvanV gravatar image IvanV  ( 2014-05-14 04:50:12 -0600 )edit

what does "echo $PYTHONPATH" print?

joq gravatar image joq  ( 2014-05-14 05:08:17 -0600 )edit

> echo $PYTHONPATH
/home/ivan/catkin_workspace/devel/lib/python2.7/dist-packages:/opt/ros/groovy/lib/python2.7/dist-packages:/home/ivan/catkin_workspace/install/lib/python2.7/dist-packages

IvanV gravatar image IvanV  ( 2014-05-14 20:52:09 -0600 )edit

That looks OK. It might help for you to edit your original question, adding relevant parts of your CMakeLists.txt.

joq gravatar image joq  ( 2014-05-15 04:11:40 -0600 )edit
joq gravatar image joq  ( 2014-05-15 04:13:29 -0600 )edit

Thank you for your suggestions. I will edit the question adding the CMakeLists.txt.

IvanV gravatar image IvanV  ( 2014-05-15 04:52:13 -0600 )edit

Adding this might help: ``catkin_install_python(PROGRAMS scripts/usv_comm.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})``

joq gravatar image joq  ( 2014-06-03 10:24:20 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted
2

answered 2016-06-20 17:58:28 -0600

jdlangs gravatar image

For anyone else who has this same problem and finds this question first as I did, the answer was discovered here: http://answers.ros.org/question/17350...

Basically, python gets confused if your script has the same name as the package and so it can't find the service import. If you rename your script, also be sure to delete the leftover .pyc file from the old name. Hopefully this will save someone the 15 min it took me to discover this.

edit flag offensive delete link more
1

answered 2017-05-23 12:31:29 -0600

AndyZe gravatar image

You'll also get a ImportError: No module named... error if the module is named incorrectly. For example, from the Python tutorial:

from beginner_tutorials.srv import *

^beginner_tutorials is the package name, not the name of the srv file.

edit flag offensive delete link more

Comments

Afaict this is actually correct: beginner_tutorials.srv is the module, as service classes are generated and placed in the module srv in the module beginner_tutorials.

gvdhoorn gravatar image gvdhoorn  ( 2017-05-23 15:36:14 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2014-05-13 23:18:59 -0600

Seen: 7,611 times

Last updated: May 23 '17