[Tutor] First line of a python program

Steve Willoughby 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
for you:

  $ /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:

  $ myscript

(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.

HTH
HAND
> 
> What is this for or do for the program?
> 
> Thanks in advance,
> 
> Katt
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

-- 
Steve Willoughby    |  Using billion-dollar satellites
steve at alchemy.com   |  to hunt for Tupperware.


More information about the Tutor mailing list