I'll first show you how to use IDLE. You will need to open a shell to log into the server machine, then type idle.
> ssh elric Password: Last login: Tue Feb 3 10:03:40 2004 from 192.168.4.156 Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994 The Regents of the University of California. All rights reserved. FreeBSD 5.2-RELEASE (GENERIC) #0: Sun Jan 11 04:21:45 GMT 2004 > idleClick on "File" then "New Window" to create a new text edit window. This lets you type in any text. Paste in one of the examples then choose "File" followed by "Save". It asks for a file name so use the one I put in the comment.
Now that it's saved, click on "Run" then "Run Module". This runs the program and puts the output in the "*Python Shell*" window.
If I made a typing mistake then usually the program won't run and Python will gave an error message telling me where the problem was. If the error is hard to diagnose I can also ask idle to help debug the code. First turn on the debugger with "Debug" -> "Debugger". Then when I run the program again I am in the debugger.
If you want to learn more about Idle, here's some documentation.
By convention, Python script files end with the extension .py.
Most programmer editors have a Python mode that will auto-format your Python programs and highlight keywords. Python mode is usually activated automatically for files which end in .py. Many programs will use Python mode if the word "python" appears in the first line or the line
# -*- mode: python -*-is in the first few lines of the program. You may have to save and reopen the file for this to work.
> python now.py Now is 02-02-2004 at 19:55 or, more precisely, 2004-02-02 19:55:43.046953 >
#!/usr/bin/env python # now.py import datetime now = datetime.datetime.now() print "Now is", now.strftime("%d-%m-%Y"), "at", now.strftime("%H:%M") print "or, more precisely, %s" % nowMake the script executable with chmod +x now.py
> chmod +x now.pyThen run the program as if it's any other Unix program
> now.py Now is 02-02-2004 at 19:55 or, more precisely, 2004-02-02 19:55:43.046953 >
Here are some of the errors you might get in Python
>>> import date Traceback (most recent call last): File "
", line 1, in ? ImportError: No module named date >>> >>> >>> import datetime >>> datetime.now Traceback (most recent call last): File " ", line 1, in ? AttributeError: 'module' object has no attribute 'now' >>> >>> >>> datetime.datetime.now >>> >>> 1+ File " ", line 1 1+ ^ SyntaxError: invalid syntax >>> >>> print "Now is, now.strftime("%d-%m-%y") File " ", line 1 print "Now is, now.strftime("%d-%m-%y") ^ SyntaxError: invalid syntax >>> prin "Hello!" File " ", line 1 prin "Hello!" ^ SyntaxError: invalid syntax >>>
This page based on Perl Script Mechanics.