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

No module named srv

asked 2013-01-19 04:22:11 -0600

dptsrk gravatar image

updated 2014-01-28 17:14:54 -0600

ngrennan gravatar image

I was following the tutorial WrittingServiceClient(Python). I have typed the code as it is. The project "makes" properly also, but when I try to rosrun the server or the client the following error shows:

from beginner_tutorials import * ImportError: No module named srv

Where am I going wrong? How to create the module srv?

N.B.: I have followed the tutorial "Creating a ROS msg and srv" and the srv file exists inside the beginner_tutorials/srv

edit retag flag offensive close merge delete

Comments

Did you sort it out? I am having exactly the same problem...

mmodetb gravatar image mmodetb  ( 2015-03-07 15:49:02 -0600 )edit

I encounter the same problem right now, has anybody found why it happens ?

dkurzaj gravatar image dkurzaj  ( 2016-07-27 07:20:33 -0600 )edit

In my case, I had to source the devel/setup.bash in each instance of the console I was calling the scripts from. Once for the client, once for the server. Then, it worked for me.

mneumann gravatar image mneumann  ( 2018-09-14 09:40:48 -0600 )edit

5 Answers

Sort by » oldest newest most voted
11

answered 2016-08-01 04:52:36 -0600

dkurzaj gravatar image

I had this error because of module name conflict.

e.g.:

catkin_ws/
  | src/
    | package/
      | srv/
        | service.srv
      | scripts
        | package.py
        | code_using_service.py

I was importing the service into the code_using_service.py file and got the error message:

from package.srv import *
ImportError: No module named srv

It was looking for the srv module inside my package.py module instead of inside my package/ module. So I just renamed my script file to package_node.py to avoid the conflict.

edit flag offensive delete link more

Comments

4

This was my problem. To fix it I had to not only rename the script, but also make sure any pyc files were removed.

vschmidt gravatar image vschmidt  ( 2017-01-31 17:38:23 -0600 )edit

Service name and package name can be the same, right?

Jägermeister gravatar image Jägermeister  ( 2019-07-29 06:55:24 -0600 )edit

This solved it for me. I had a .py file named the same as the package name, which then made the script try to import the python file, and not the .srv file from the /srv folder. Thank you

traemand gravatar image traemand  ( 2021-04-06 08:05:15 -0600 )edit
3

answered 2013-01-19 06:19:20 -0600

130s gravatar image

In the tutorial Writing a Simple Service and Client (Python), do Build your nodes section and rosrun again. It seems to me you might not have generated *.srv modules. In that tutorial page, don't forget to choose your build system (if you're using rosmake, choose rosbuild).

edit flag offensive delete link more
2

answered 2013-01-19 06:32:50 -0600

KruseT gravatar image

The module srv is a python module, which in practice just means a folder named srv with a __init__.py in it. This file is generated from .srv files you define, when building the project correctly.

With catkin, the safest way to rebuild everything is to delete your build and devel folders and rebuild everything.

edit flag offensive delete link more

Comments

I don't think this is an answer. You can remove those folders and rebuild everything, there is no guarantee that it will work. One has to check CMakeLists.txt, package.xml and the code files, all should be consistent with the package name and all that.

Jägermeister gravatar image Jägermeister  ( 2019-08-05 09:29:09 -0600 )edit
0

answered 2022-05-09 14:16:20 -0600

TheAlmightySand gravatar image

I had the same problem. It turned out I had forgotten to uncomment this part of my CMakeLists.txt:

add_service_files( FILES AddTwoInts.srv )

edit flag offensive delete link more
0

answered 2020-08-20 12:11:38 -0600

hamdi sharaf gravatar image

i had same problem , I had to source the( devel/setup.bash) in each instance of web shell I was calling the scripts from. Once for the client, once for the server. Then, it worked

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-01-19 04:22:11 -0600

Seen: 17,912 times

Last updated: Aug 01 '16