Ask Your Question

Python Import not being recognized

asked 2016-07-24 20:43:26 -0500

Starmit gravatar image

Hi guys,

I'm currently working through a textbook call Programming Robots with ROS. There is a chapter that goes over a simple movement code for a turtlebot simulator. The code is pasted below.

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist

cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1) #<1>

red_light_twist = Twist() #<2>
green_light_twist = Twist()
green_light_twist.linear.x = 0.5 #<3>

driving_forward = False
light_change_time =
rate = rospy.Rate(10)

while not rospy.is_shutdown():
  if driving_forward:
    cmd_vel_pub.publish(green_light_twist) #<4>
  if > light_change_time: #<5>
    driving_forward = not driving_forward
    light_change_time = + rospy.Duration(3)
  # END PART_1
  rate.sleep() #<6>

Nothing seems to be wrong with the code itself. However, when I run

chmod +x

followed by

./ cmd_vel:=cmd_vel_mux/input/teleop

I receive the following errors:

./ line 3: import: command not found
from: can't read /var/mail/geometry_msgs.msg
./ line 6: syntax error near unexpected token `('
./ line 6: `cmd_vel_pub = rospy.Publisher('cmd_vel', Twist, queue_size=1) #<1>'

I'm not clear to the second and third error, but the firs error is claiming to not be able to find import rospy. Any assistance would be greatly appreciated.


edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-07-24 23:14:49 -0500

ahendrix gravatar image

The first error is import: command not found; this means that the import command itself is unrecognized; not the library being imported. This usually happens when you try to run a python program with the shell.

When you execute a program with ./, it looks at the first line, and if it starts with #!, it uses the rest of the line as the program for executing the file. If the file doesn't start with a #!, it's executed with the default shell. It looks like that's what happing here.

I would double-check your file and make sure that the very first line is #!/usr/bin/env python

edit flag offensive delete link more



Yep, delete the line # BEGIN ALL and make sure #!/usr/bin/env python is the first text in the file.

Airuno2L gravatar image Airuno2L  ( 2016-07-25 07:29:49 -0500 )edit

answered 2018-07-29 14:32:02 -0500

KFW gravatar image

Apparently the #BEGIN ALL is part of the mark-up they had to use to include code samples in the book - should not be in the actual code files. See /K

edit flag offensive delete link more

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


Asked: 2016-07-24 20:43:26 -0500

Seen: 3,147 times

Last updated: Jul 24 '16