March 31st, 2010, 06:45 AM
Can't run a .py file from the command line
I am a beginner and I am new to programming. I have just started learning python from a book called 'think python'. There is a segment in the book where the author has asked us to download certain modules which he has online. I did the same and now when I try to run those modules using the command line, I am unable to do so.
The files name is TurtleWorld.py. When I type in the command line:
python TurtleWorld.py it says 'invalid syntax'
I don't know what am I doing wrong here. I tried importing the module as well, by typing in the command line: import Turtleworld
This does not give me any error. Please help me out. I want to be able to run the file from the command line.
March 31st, 2010, 07:45 AM
paste the code. there is an error in it...
March 31st, 2010, 07:54 AM
here's the code for the file TurtleWorld.py:-
from World import *
"""an environment for Turtles and TurtleControls
def __init__(self, interactive=False):
# keep track of the most recent world
TurtleWorld.most_recent = self
# the interpreter executes user-provided code
self.inter = Interpreter(self, globals())
# delays is time in seconds to sleep after an update
self.delay = 0.05
"""create the GUI"""
# canvas width and height
self.ca_width = 400
self.ca_height = 400
self.canvas = self.ca(width=self.ca_width,
"""create the right frame with the buttons for interactive mode
# right frame
self.gr(2, [1,1], [1,1], expand=0)
self.bu(text='Print canvas', command=self.canvas.dump)
self.bu(text='Make Turtle', command=self.make_turtle)
# run this code
self.bu(side=BOTTOM, text='Run code', command=self.run_text, expand=0)
self.te_code = self.te(height=10, width=25, side=BOTTOM)
self.te_code.insert(END, 'bob = Turtle(world)\n')
# run file
self.row([0,1], pady=30, side=BOTTOM, expand=0)
self.bu(side=LEFT, text='Run file', command=self.run_file)
self.en_file = self.en(side=LEFT, text='turtle_code.py', width=5)
# leave the right frame open so that Turtles can add TurtleControls
"""add a row of buttons for run, step, stop and clear"""
self.gr(2, [1,1], [1,1], expand=0)
"""create a new turtle and corresponding controller"""
turtle = Turtle(self)
control = TurtleControl(turtle)
turtle.control = control
"""undraw and remove all the animals, and anything else
on the canvas
for animal in self.animals:
if hasattr(animal, 'control'):
self.animals = 
"""represent a Turtle in a TurtleWorld
def __init__(self, world=None, delay=0.1):
"""a Turtle has a radius (r), heading, pen (True/False for
active/inactive), and a color.
if world == None:
world = TurtleWorld.most_recent
self.r = 5
self.heading = 0
self.pen = True
self.color = 'red'
self.delay = delay
"""default step behavior is forward one pixel"""
def draw_line(self, scale, dtheta, **options):
"""draw a line through the center of this turtle,
with a dtheta angle from the turtle's heading,
and length 2r"""
r = scale * self.r
theta = self.heading + dtheta
head = self.polar(self.x, self.y, r, theta)
tail = self.polar(self.x, self.y, -r, theta)
self.world.canvas.line([tail, head], **options)
"""draw the turtle"""
self.tag = 'Turtle%d' % id(self)
lw = self.r/2
# draw the line that makes the head and tail
self.draw_line(2.5, 0, tags=self.tag, width=lw, arrow=LAST)
# draw the diagonal that makes two feet
self.draw_line(1.8, 40, tags=self.tag, width=lw)
# draw the diagonal that makes the other two feet
self.draw_line(1.8, -40, tags=self.tag, width=lw)
# draw the shell
self.world.canvas.circle([self.x, self.y], self.r, self.color,
def fd(self, dist=1):
"""move the turtle foward by the given distance"""
x, y = self.x, self.y
p1 = [x, y]
p2 = self.polar(x, y, dist, self.heading)
self.x, self.y = p2
# if the pen is down, draw a line
self.world.canvas.line([p1, p2], fill='black')
def bk(self, dist=1):
"""move the turtle backward by the given distance"""
def rt(self, angle=90):
"""turn right by the given angle"""
self.heading = self.heading - angle
def lt(self, angle=90):
"""turn left by the given angle"""
self.heading = self.heading + angle
"""put the pen down (active)"""
self.pen = True
"""put the pen up (inactive)"""
self.pen = False
def set_color(self, color):
"""change the color of the turtle and redraw.
Note that changing the color attribute doesn't change the
turtle on the canvas until redraw is invoked. One way
to address that would be to make color a property.
self.color = color
"""some turtles have a turtle control panel in the GUI"""
def __init__(self, turtle):
self.turtle = turtle
w = self.turtle.world
self.frame = w.fr(bd=2, relief=SUNKEN, padx=1, pady=1, expand=0)
# forward and back (and the entry that says how far)
w.bu(side=LEFT, text='bk', command=Callable(self.move_turtle, -1))
self.en_dist = w.en(side=LEFT, fill=NONE, expand=0, width=5, text='10')
w.bu(side=LEFT, text='fd', command=self.move_turtle)
# other buttons
w.bu(side=LEFT, text='lt', command=self.turtle.lt)
w.bu(side=LEFT, text='rt', command=self.turtle.rt)
w.bu(side=LEFT, text='pu', command=self.turtle.pu)
w.bu(side=LEFT, text='pd', command=self.turtle.pd)
colors = 'red', 'orange', 'yellow', 'green', 'blue', 'violet'
# color menubutton
self.mb = w.mb(text=colors)
for color in colors:
w.mi(self.mb, text=color, command=Callable(self.color, color))
def color(self, color):
"""callback for the menu button: change the color of the
turtle and the text on the button"""
def move_turtle(self, sign=1):
"""callback for fd and bk buttons: read the entry and move
the turtle. sign should be +1 or -1 for fd or back."""
dist = int(self.en_dist.get())
# add the turtle methods to the module namespace
# so they can be invoked as simple functions (not methods)
fd = Turtle.fd
bk = Turtle.bk
lt = Turtle.lt
rt = Turtle.rt
pu = Turtle.pu
pd = Turtle.pd
die = Turtle.die
set_color = Turtle.set_color
"""invoke mainloop on the most recent TurtleWorld
if __name__ == '__main__':
world = TurtleWorld(interactive=True)
All the other modules it is referring to within this code are present in the same location as the file itself. However like I said, when I try running this file using the command line it says: invalid syntax.
Please help me out. I want to understand what am I doing wrong here.
March 31st, 2010, 08:43 AM
We'll need to know in which line the error occurs (the error message will say so), and if you'd point out that line, it would be nice. You don't expect us to search ca. 250 lines for a subtle syntax error, do you? (and no, I can't try it out myself, I got only python 3.1 installed and as far as google tells me, this program is written for python 2.4 - there will be tons of syntax errors due to that)
One guess I can make: Maybe you have a higher version of Python than 2.4 installed (like I have) - very likely that causes errors.
March 31st, 2010, 09:37 AM
Sorry I did not mention the line number. It says syntax error on line 1.
And you are right I am using python 2.6.
Is that the reason for this error? Can I get rid of this error and still continue using python 2.6 or should I change my version for that?
March 31st, 2010, 09:53 AM
Line 1 of TurtleWorld.py ? from module import * is perfectly fine python syntax. Watch out: The error message may name several files/lines, the thing that actually caused the error is the last one. The other ones are the just the traceback (how the program got to the point where it failed). The error lies somewhere else, very likely in World.py or some other file included there or ... etc.
About the version: As already written, this program is seemingly for version 2.4 - if you can get your hands on it, you may want to use it, but otherwise, 2.6 should be fine, too. I don't think there are any backwards-incompatible changes in it...
April 1st, 2010, 11:57 AM
The current directory is not in your search path. On a Linux system it would be
or you could enter the complete path
or you could move TurtleWorld.py to a location that is in your path (print sys.path to find out what directories are in PYTHONPATH).
April 1st, 2010, 12:09 PM
Wrong. Just plain wrong. When you try to start a file that's not there, it says something like:
And if you try to import a module and it can't find it, it raises and ImportError, not a SyntaxError.
Last edited by delnan; April 1st, 2010 at 12:12 PM.
April 2nd, 2010, 10:49 PM
I am still struggling with this. Any suggestions on what I could do differently to make this work from the command line?
April 3rd, 2010, 03:59 AM
You read this? If yes, is the syntax error really the last (lowermost) error message?
Originally Posted by delnan
April 3rd, 2010, 04:18 AM
Yes it is...It's the lowermost and the only error message!!! Please help.
April 3rd, 2010, 04:31 AM
Well, only advice I got left... re-install Python (and try to get 2.4), if it consideres "from module import *" a syntax error, it's hopelessly broken.