21

Possible Duplicate:
Why do people write #!/usr/bin/env python on the first line of a Python script?
What does the line “#!/bin/sh” mean in a UNIX shell script?

It's a comment and should be ignored by the interpreter. Why is #!/usr/bin/python still in almost every program?

Community
  • 1
  • 1
NoobDev4iPhone
  • 4,757
  • 10
  • 28
  • 33

3 Answers3

22

If the script is made executable, the operating system will use that first line to know which interpreter to run to parse the rest of the file to perform some action.

If you always run these kinds of scripts from the command line like python foo.py then you could remove the first line.

iandouglas
  • 3,809
  • 2
  • 30
  • 33
  • 5
    That applies only to Unix-like operating systems. Windows, for example, doesn't do this; it typically uses the `.py` suffix to determine how to execute the script. – Keith Thompson Mar 28 '12 at 04:18
4

Not actually. It is tradition in Unix like operating systems to use the interpreter path given in the first line and use the path to that interpreter for interpreting the rest of the program. It is called as Shebang line. You would not cared for it if you were not on Unix like systems (linux. Mac OS, FreeBSD etc) but there are attempts in Python community to use similar shebang lines on windows too.

Senthil Kumaran
  • 47,625
  • 13
  • 83
  • 117
1

Read the Wikipedia article Shebang. Also remember outside the *nix world, the first shebang prefixed line is considered as a comment.

Abhijit
  • 55,716
  • 14
  • 105
  • 186