[Tutor] First line of a python program
steve at alchemy.com
Tue Oct 13 01:07:27 CEST 2009
On Mon, Oct 12, 2009 at 02:49:34PM -0700, Katt wrote:
> Hello all,
> Numerous times I see the following as the first line of a python program:
> #! /usr/bin/python
As far as Python is concerned, it is a comment.
Anything from the # character to the end of the line is a comment.
If you want to execute this script, you need to run the Python
interpreter and tell it to load the script file and run it
$ /usr/bin/python myscript
You could just set your script to be executable (by setting the
right permission bits) and then you can run it as a command
without naming python yourself:
(that assumes it's in a directory in your PATH, of course,
or you'd have to type the path to the script as well.)
That allows you to write scripts which act like any other
command on your system.
In order to do that, though, the system needs to know what interpreter
to use to actually run your script. Unix uses the convention of looking
at the first few bytes of the program file. If they start with "#!" then
the remainder of the first line is assumed to be the name of the interpreter
program, so with "#!/usr/bin/python" at the top of your file, Unix will
know to run it as "/usr/bin/python myscript".
If you use an operating system other than Unix, or invoke the interpreter
yourself (typing "python myscript"), then this line is completely ignored.
> What is this for or do for the program?
> Thanks in advance,
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
Steve Willoughby | Using billion-dollar satellites
steve at alchemy.com | to hunt for Tupperware.
More information about the Tutor