355/113
Posts: 26
Joined: Wed Aug 22, 2012 2:22 am

#!/usr/bin/python2 why?

Wed Mar 05, 2014 11:38 pm

I am just learning python and have noticed that the first line in many examples looks something like this:

Code: Select all

#!/usr/bin/python2
I thought the # sign meant the entire line was a comment; now I'm seeing that maybe this is a 'symlink?' telling the compiler where stuff is? No?

What's it all about?

User avatar
DougieLawson
Posts: 41300
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: #!/usr/bin/python2 why?

Wed Mar 05, 2014 11:44 pm

Hash (aka pound, aka sharp, aka octothorpe) followed by an exclamation mark (aka exclamation point) is a special construction called a "shebang". http://en.wikipedia.org/wiki/Shebang_%28Unix%29

When bash finds that shebang in the first line of a script it uses to 'exec' the program name following the #! and send the script file to stdin. The interpreter program (hopefully) can make sense of what comes in on it's input and does the magic to get the script running and doing its magic.
Last edited by DougieLawson on Wed Mar 05, 2014 11:45 pm, edited 1 time in total.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.


355/113
Posts: 26
Joined: Wed Aug 22, 2012 2:22 am

Re: #!/usr/bin/python2 why?

Thu Mar 06, 2014 3:45 pm

Thanks for the explanation! It was worth getting out of bed this morning - I have learned something already.

Now my question is what is the purpose when using it in a python program? So far, most of the tutorial example snippets I have seen don't include it. When does a programmer include it and when not? My understanding so far to start a python code file from the command line, I type "python somefile.py" and off it runs. So although I see that the #! is a special character to allow running some other program first, what sort of program is typically used in this fashion?

User avatar
joan
Posts: 15616
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: #!/usr/bin/python2 why?

Thu Mar 06, 2014 4:06 pm

355/113 wrote:Thanks for the explanation! It was worth getting out of bed this morning - I have learned something already.

Now my question is what is the purpose when using it in a python program? So far, most of the tutorial example snippets I have seen don't include it. When does a programmer include it and when not? My understanding so far to start a python code file from the command line, I type "python somefile.py" and off it runs. So although I see that the #! is a special character to allow running some other program first, what sort of program is typically used in this fashion?
If module contains the #!/usr/bin/python and has the executable bit set it can be invoked by

./module

as well as

python module

Return to “Python”