October 28th, 2005, 06:01 PM
the #!usr/bin/..... thing
What is the difference between
Is it the same which one you use, or does it depend on which os you are programming in?
Or am i way off?
another n00b question from me
October 28th, 2005, 06:03 PM
It is defining the location (on a Linux system) that your Python packages are installed. I believe it is usually located in:
Apparently you can use multiple methods of including this directory.
October 28th, 2005, 06:21 PM
/usr/bin/python and /usr/local/bin/python are where the python executable is. So - when you try to run your script, and the shell will look at the first line, and use that shebang line to work out what to parse the file with. Others include #!/bin/bash ( bash shell scripting ), #!/usr/bin/perl ( perl ) etc.
The /usr/bin/env python is slightly different in that it says "use the first python executable you find in the users environment". This is probably the best form to use.
Comments on this post
October 30th, 2005, 08:14 AM
okay, so i will use the .../env python .
Thanks for helping a noob
October 31st, 2005, 06:15 PM
Note that this only applies to Unixoid systems such as Linux, FreeBSD, MacOS X, etc; Windows and earlier version of MacOS have different ways of determining how to run a script.
October 31st, 2005, 09:50 PM
ya, this only happens from the sh or bash interpriter, well others like csh just about anything that is sorta designed to be a *nix scripting language its called a "shebang" it is designed to be a comment if it is executed directly by the approperate app but if it is executed u*sh would know what to do with it. Just a way of determining where to run an app from.
November 1st, 2005, 11:08 AM
Ok, so i'll have to use the #!/usr/bin/python for windows then?
I'm kinda confused?
but, then again I've just started with python and have no other programming experience (except some basic html )
November 1st, 2005, 11:48 AM
no that thing is for linux programs and as far as I understand it does not matter I dont know for sure though, because it is just a comment in the file
Those people who think they know everything are a great annoyance to those of us who do.
November 1st, 2005, 01:58 PM
The reason behind the #!/usr... line (otherwise called the "shebang" line) is because of how Unix shells work. As Schol-R-Lea stated above, this line does not need to be there for Windows or older Mac systems (but it doesn't hurt to be there either, as will be explained below).
Here's how it works:
First, note that the # character in python (and also in perl) is used to indicate a comment. Thus, anything after a # character will be skipped by the python interpreter. Hence, a python interpreter will actually treat this line as a comment:
because of the # sign. This line is never processed by the python interpreter, whether on a UNIX-like system or a Windows system.
As it happens, all UNIX systems treat the sequence #! as a magic set of characters that tell what program to execute to process the rest of the script. This has been the case for 25 years now: See this post by Dennis Ritchie (dmr) back in 1980:
when he first programmed this feature in. So #!/usr/bin/env tells the system to execute /usr/bin/env and #!/usr/bin/env python tells the system to execute /usr/bin/env and pass the word "python" as argument to this program. The net effect of this is that /usr/bin/env runs first, then it looks for the python interpreter in the user's environment and locates the first working copy. Then /usr/bin/env in turn executes the python interpreter and passes it the file to process.
Knowing this fact, you can probably see why the designers of both perl and python chose to make # as the comment character. Since the first line contains the shebang line, which is actually a command to the shell telling it to run the python interpreter, this line should not itself be processed by the python interpreter again. It is a shell command and not part of the python language. Thus, the # in the #! line causes the python interpreter to treat that line as a comment when processing the file.
On a windows system, the command shell works a bit differently. It uses the file extension to determine what program should run, instead of the shebang line. Hence, the shebang line is not needed because the Windows command shell knows to bring up the python interpreter based on the .py file extension. Not that it makes a difference if it is present or not, because the python interpreter ignores that line anyway (as it does in a UNIX-like system) because it is a comment, as far as python is concerned.
As to #!/usr/bin/python vs. #!/usr/local/bin/python, you are assuming that the python interpreter is located in /usr/bin/ or in /usr/local/bin. This is a rather bad assumption to make because some UNIXes (Linux distros like Red Hat, Fedora etc.) put python in the /usr/bin directory and others (such as OpenBSD and NetBSD) will install it in /usr/local/bin, if installed from packages or port. On top of that, any user may install their own version of python in their local directory as well. Hence, you can see that it is not a good idea to assume where the python interpreter is located. So what's the solution then? The solution is to use /usr/bin/env and make it locate the version of python (whether it is installed in /usr/bin or /usr/local/bin or elsewhere). UNIX systems may differ as to where the python interpreter is stored, but they mostly have env in the same place (i.e.) in /usr/bin/env. This is why you should use #!/usr/bin/env python if you want your program to run on many platforms without any modifications.
Comments on this post
Last edited by Scorpions4ever; November 1st, 2005 at 02:05 PM.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
November 1st, 2005, 02:43 PM
Aw man, thanks alot for a great answer. thumbs up