0
$\begingroup$

Rosanswers logo

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


Originally posted by IvanV on ROS Answers with karma: 329 on 2014-05-13

Post score: 2


Original comments

Comment by joq on 2014-05-14:
Did your package build successfully? What is in your ~/catkin_ws/devel/lib/python2.7/dist-packages/usv_comm/srv folder?

Comment by IvanV on 2014-05-14:
Yes, the package build successfully. In that folder there are two files: init.py and _sendTwist.py.

Comment by joq on 2014-05-14:
what does "echo $PYTHONPATH" print?

Comment by IvanV on 2014-05-14:\

> 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

Comment by joq on 2014-05-15:
That looks OK. It might help for you to edit your original question, adding relevant parts of your CMakeLists.txt.

Comment by joq on 2014-05-15:
You might also check with this page: http://docs.ros.org/api/catkin/html/howto/building_msgs.html .

Comment by IvanV on 2014-05-15:
Thank you for your suggestions. I will edit the question adding the CMakeLists.txt.

Comment by joq on 2014-06-03:
Adding this might help: catkin_install_python(PROGRAMS scripts/usv_comm.py DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION})

Comment by Mehdi. on 2015-08-14:
I also have the same problem, my Python IDE can find the srv, rossrv show can also find the srv, but when doing rosrun I get the same error message. I even changed the name of the node to something different than the package name (also leads to a similar error) but still nothing. Any suggestions?

Comment by akash_p on 2017-11-07:
Any updates on this issue?I did everything mentioned here still getting same error.

Comment by IvanV on 2017-11-07:
@akash_p My problem was actually what is mentioned in the first answer: the script had the same name as the package and confused the import. I changed the name of the script and everything worked. If that doesn't fix it four you, maybe you should create a new question with your specific case details

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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/173507/cannot-import-custom-service/

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.


Originally posted by jdlangs with karma: 971 on 2016-06-20

This answer was ACCEPTED on the original site

Post score: 2

$\endgroup$
0
$\begingroup$

Rosanswers logo

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.


Originally posted by AndyZe with karma: 2331 on 2017-05-23

This answer was NOT ACCEPTED on the original site

Post score: 1


Original comments

Comment by gvdhoorn on 2017-05-23:
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.