Rosanswers logo

While doing the example of Service and Client from this page, I am getting an error "Import Command not Found" when trying to run the Server and the Client Nodes. I have checked the code but evrything is acoording to the tutorial. Can somebody please help? I am doing these examples using rospy.


The file is as below :

#!/usr/bin/env python

import rospy

from beginner_tutorials.srv import *

def handle_add_two_ints(req):
  print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b))
  return AddTwoIntsResponse(req.a + req.b)

def add_two_ints_server():
  s = rospy.Service('add_two_ints', AddTwoInts, handle_add_two_ints)
  print "Ready to add two ints."

if __name__ == "__main__":

Originally posted by ish45 on ROS Answers with karma: 151 on 2014-08-21

Post score: 2

Original comments

Comment by ish45 on 2014-08-21:
Yes. I have run catkin_make and source devel/setup.bash. Infact, I have been able to sucessfully run the publish, subscribe messages tutorial in python. So, I guess python has been set up properly.

Edit : python filename.py as suggested by one of the users has worked without any errors.


1 Answer 1


Rosanswers logo

Have you remembered to do a catkin_make, then source devel/setup.bash before running the examples?

Without the proper environment setup, Python is not going to be able to find the ROS imports.


Right. Two things: please don't use answers to answer someone else's answer (:)). Use comments for that. Also: please update your original question with the contents of the file you are trying to run (please use formatting for that: indent every line with 4 spaces).

Lastly: searching for Import Command not Found shows a lot of pages recommending me to make sure I have #!/usr/bin/env python at the top of my file. Do you? The error is probably really import: command not found, right?

Edit2: I've fixed the formatting a bit.

  1. Can you confirm that your file is exactly like this?
  2. How do you exactly run your node?
  3. Have you made your file executable (ie: chmod +x filename.py)?
  4. What is the output if you do python filename.py (change filename.py to the actual name)
  5. Did you by any chance create the file in Windows (running Ubuntu in a VM)? If so, could you try fromdos filename.py and see if the console output changes?

Originally posted by gvdhoorn with karma: 86574 on 2014-08-21

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by ish45 on 2014-08-21:
Yes, I have added that line at the top of the file as you can see. But, it is still giving the error. The error is "Import: Command Not Found". Don't know what is missing.

Comment by gvdhoorn on 2014-08-21:
Are the line numbers also in your file? Those are not supposed to be there. Please make sure the question contains a direct copy/paste from the file you are trying to execute.

Comment by ish45 on 2014-08-21:
No. The line numbers are not there in the file.

Comment by ish45 on 2014-08-21:
Thanks. python filename.py command has worked. Strangely, rosrun did not seem to be running. Thanks again.

Comment by gvdhoorn on 2014-08-21:
Could you answer all 5 questions please? The fact that python filename.py worked is just an indication that something isn't right with your file, and it is most likely the shebang line (the #!/usr/bin/env python).

Comment by ish45 on 2014-08-21:\

  1. Yes the file is exactly like this
  2. rosrun [package name] [executablefile.py]
  3. Yes I have run chmod +x filename.py
  4. Yes, I have ubuntu installed on VB. fromdos filename.py does not seem to do anything. No output.

Comment by gvdhoorn on 2014-08-21:
But did you use a windows editor to create the file (via a shared folder fi)? fromdos filename.py is not supposed to give any output. It converts line-endings from DOS/Windows files into Unix line-endings. Could you now (after having done fromdos filename.py) try just doing ./filename.py now, or even rosrun pkg filename.py?


Your Answer

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