#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    27
    Rep Power
    0

    the #!usr/bin/..... thing


    What is the difference between
    #!/usr/bin/env python
    and
    #!/usr/bin/python
    and
    #!/usr/local/bin/python
    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
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    It is defining the location (on a Linux system) that your Python packages are installed. I believe it is usually located in:
    Code:
    /usr/bin/env
    Apparently you can use multiple methods of including this directory.
  4. #3
  5. (retired)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2003
    Location
    The Laboratory
    Posts
    10,101
    Rep Power
    0
    /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.

    --Simon

    Comments on this post

    • E D D ! 3 agrees
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    27
    Rep Power
    0
    okay, so i will use the .../env python .

    Thanks for helping a noob
  8. #5
  9. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    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.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short Understanding the C/C++ Preprocessor
    Taming Python A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    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.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    27
    Rep Power
    0
    Ok, so i'll have to use the #!/usr/bin/python for windows then?
    or #!/usr/local/bin/python?
    I'm kinda confused?
    but, then again I've just started with python and have no other programming experience (except some basic html )
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    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.
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    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:
    #!/usr/bin/env python
    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:
    http://www.in-ulm.de/~mascheck/vario...ng/sys1.c.html
    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

    • SimonGreenhill agrees
    Last edited by Scorpions4ever; November 1st, 2005 at 01: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
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    27
    Rep Power
    0
    Aw man, thanks alot for a great answer. thumbs up

IMN logo majestic logo threadwatch logo seochat tools logo