Is "#!/usr/bin/env python" the better shebang line ?

Discussion in 'Python' started by Timothy Madden, Sep 6, 2009.

  1. Hello

    Sorry if this has been discussed before, my search did not find it.
    My questions is if I should use
    #!/usr/bin/env python
    as the shebang line in a portable and open python script and if it does
    help with portability and usage.

    First, can one not find /usr/bin/python in any standard system, at least
    as much as /usr/bin/env can be found ?

    Then, supposing /usr/bin/env is better than /usr/bin/python and I use
    it, is it not the case that many editors and code analysing programs,
    that want to know the script language for syntax highlighting, code
    completion, tags and source code browsing, etc, will loose their
    functionality because of this shebang line ? Is this not a good general
    reason to use the old one ?

    Now I know env is POSIX standard utility, and python is not, and maybe
    that could be reason enough, but are there any other issues involved
    with using /usr/bin/env ?

    Thank you
    Timothy Madden
    Timothy Madden, Sep 6, 2009
    #1
    1. Advertising

  2. On Sun, Sep 6, 2009 at 10:01 AM, Timothy Madden<> wrote:
    > Hello
    >
    > Sorry if this has been discussed before, my search did not find it.
    > My questions is if I should use
    >  #!/usr/bin/env python
    > as the shebang line in a portable and open python script and if it does help
    > with portability and usage.
    >
    > First, can one not find /usr/bin/python in any standard system, at least as
    > much as /usr/bin/env can be found ?
    >

    Not necessarily. The system python (if it exists) is usually in
    /usr/bin but what if there is no system-installed Python or the user
    would prefer to use a version they compiled themselves and is
    therefore in /usr/local/bin? Or what if the system decides /usr/bin is
    just for the default system tools and everything else should be
    installed in /opt/? env will always be in the same place, but Python
    may not be so using env makes it more portable.


    > Then, supposing /usr/bin/env is better than /usr/bin/python and I use it, is
    > it not the case that many editors and code analysing programs, that want to
    > know the script language for syntax highlighting, code completion, tags and
    > source code browsing, etc, will loose their functionality because of this
    > shebang line ? Is this not a good general reason to use the old one ?
    >
    > Now I know env is POSIX standard utility, and python is not, and maybe that
    > could be reason enough, but are there any other issues involved with using
    > /usr/bin/env ?
    >
    > Thank you
    > Timothy Madden
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Benjamin Kaplan, Sep 6, 2009
    #2
    1. Advertising

  3. Timothy Madden

    Ned Deily Guest

    In article <4aa3bfdf$0$282$>,
    Timothy Madden <> wrote:
    > My questions is if I should use
    > #!/usr/bin/env python
    > as the shebang line in a portable and open python script and if it does
    > help with portability and usage.


    This question came up recently on stackoverflow (alas, will result in
    urloverflow):

    http://stackoverflow.com/questions/1352922/why-is-usr-bin-env-python-supp
    osedly-more-correct-than-just-usr-bin-pyth/1352938#1352938

    --
    Ned Deily,
    Ned Deily, Sep 6, 2009
    #3
  4. Ned Deily wrote:
    > In article <4aa3bfdf$0$282$>,
    > Timothy Madden <> wrote:
    >> My questions is if I should use
    >> #!/usr/bin/env python
    >> as the shebang line in a portable and open python script and if it does
    >> help with portability and usage.

    >
    > This question came up recently on stackoverflow (alas, will result in
    > urloverflow):
    >
    > http://stackoverflow.com/questions/1352922/why-is-usr-bin-env-python-supp
    > osedly-more-correct-than-just-usr-bin-pyth/1352938#1352938
    >

    Ok, thank you.
    There is quite a flame since May 2008 archived on mail.python.org about
    this issue.

    #!/usr/bin/env python is clearly the choice for my scripts.

    It helps a lot with portability, and the arguments against it are based
    on the idea that /usr/bin/python is somehow better than /opt/python and
    that /opt/python is still in front of /usr/bin/python in path. Such an
    argument I do not find compelling. The thing is that people writing
    large applications for many systems need flexibility first.

    Timothy Madden
    Timothy Madden, Sep 7, 2009
    #4
  5. Timothy Madden

    ryles Guest

    On Sep 6, 10:01 am, Timothy Madden <> wrote:
    > Hello
    >
    > Sorry if this has been discussed before, my search did not find it.
    > My questions is if I should use
    >    #!/usr/bin/env python
    > as the shebang line in a portable and open python script and if it does
    > help with portability and usage.


    Note that there is one limitation of /usr/bin/env on many systems: you
    cannot supply any arguments to the interpreter.

    e.g. #!/usr/bin/env python -u

    The above will fail on many systems as env will literally try to find
    a file named "python -u".

    http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/237208

    Another, more obvious, case where you might avoid /usr/bin/env is when
    you know (or might expect in the future) that there is another Python
    version on the system which your script will not work with. Then it's
    often convenient to hard code the interpreter path. This comes up
    mostly in environments where you're not the administrator and have
    another Python version installed in a custom place which you (and any
    co-developers) have in their PATH. For example, if you wanted to share
    your script with a non-team member, then having /usr/bin/env in your
    script would force them to have to change their PATH first.

    > Then, supposing /usr/bin/env is better than /usr/bin/python and I use
    > it, is it not the case that many editors and code analysing programs,
    > that want to know the script language for syntax highlighting, code
    > completion, tags and source code browsing, etc, will loose their
    > functionality because of this shebang line ? Is this not a good general
    > reason to use the old one ?


    I personally use vim, and it will recognize either shebang, or if none
    is present (as in a typical importable module) it will notice the file
    suffix, .py. It's not an issue.
    ryles, Sep 7, 2009
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parzival
    Replies:
    4
    Views:
    57,525
    Erik Max Francis
    Nov 10, 2003
  2. Adriano Ferreira
    Replies:
    9
    Views:
    564
    Mike Meyer
    Dec 3, 2005
  3. John Salerno
    Replies:
    30
    Views:
    1,925
    Stephan Kuhagen
    Aug 10, 2006
  4. Yves Dorfsman

    #!/usr/bin/env python vs. #!/usr/bin/python

    Yves Dorfsman, May 2, 2008, in forum: Python
    Replies:
    27
    Views:
    1,958
    Tim Roberts
    May 10, 2008
  5. Gilles
    Replies:
    7
    Views:
    537
    Matej Cepl
    Sep 30, 2012
Loading...

Share This Page