#!/usr/bin/python or #!/usr/bin/env python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #!/usr/bin/python or #!/usr/bin/env python?

    I understand the difference, but I'm just curious if anyone has any
    strong feelings toward using one over the other? I was reading that a
    disadvantage to the more general usage (i.e. env) is that it finds the
    first python on the path, and that might not be the proper one to use. I
    don't know if that's a real issue most of the time, but it's at least
    something to consider.

    And is Python found in directories other than /usr/bin often enough to
    justify using the more general form?
  • Steven Bethard

    #2
    Re: #!/usr/bin/python or #!/usr/bin/env python?

    John Salerno wrote:
    I understand the difference, but I'm just curious if anyone has any
    strong feelings toward using one over the other? I was reading that a
    disadvantage to the more general usage (i.e. env) is that it finds the
    first python on the path, and that might not be the proper one to use.
    I don't see how that's any different from when the Python at
    /usr/bin/python isn't the right one to use. I know that's true at my
    university:

    $ /usr/bin/python -V
    Python 2.2.3
    $ /usr/local/python/bin/python -V
    Python 2.4.3

    With this setup, the one in /usr/bin is for the OS and is seldom
    updated, and the one in /usr/local is for users and is updated frequently.

    Sure, if someone doesn't put /usr/local/python/bin on their PATH,
    they're not going to get the right Python, but I don't write code to
    Python 2.2 anymore, so if I use ``#!/usr/bin/python`` my code will
    probably just fail.

    Obviously, in my own code, I only use ``#!/usr/bin/env python``.

    STeVe

    Comment

    • Martin v. Löwis

      #3
      Re: #!/usr/bin/python or #!/usr/bin/env python?

      John Salerno schrieb:
      I understand the difference, but I'm just curious if anyone has any
      strong feelings toward using one over the other?
      I use /usr/bin/env if I don't know what the operating system is;
      some systems don't have Python in /usr/bin. I use /usr/bin/pythonX.Y
      if I want a specific version on a specific operating system (typically
      Linux). I use /usr/bin/python when I'm too lazy to think about it
      thoroughly.

      Regards,
      Martin

      Comment

      • skip@pobox.com

        #4
        Re: #!/usr/bin/python or #!/usr/bin/env python?


        JohnI understand the difference, but I'm just curious if anyone has
        Johnany strong feelings toward using one over the other?

        #!/usr/bin/python -never

        #!/usr/bin/env python -always

        Strong enough?


        JohnI was reading that a disadvantage to the more general usage
        John(i.e. env) is that it finds the first python on the path

        That's generally the one you want. On most systems /usr/bin/python will be
        the system-provided one (e.g. the one that came with your Linux
        distribution). You might prefer a Python of more recent vintage though.
        That's commonly installed in /usr/local/bin/python and /usr/local/bin is
        generally ahead of /usr/bin in PATH.

        JohnAnd is Python found in directories other than /usr/bin often
        Johnenough to justify using the more general form?

        On my Mac I install most stuff in ~/local simply so I don't need to be
        root and don't disturb the system-provided software. In cases where other
        users need to run such tools I generally use /usr/local.

        Skip

        Comment

        • John Salerno

          #5
          Re: #!/usr/bin/python or #!/usr/bin/env python?

          skip@pobox.com wrote:
          On most systems /usr/bin/python will be
          the system-provided one (e.g. the one that came with your Linux
          distribution). You might prefer a Python of more recent vintage though.
          That's commonly installed in /usr/local/bin/python and /usr/local/bin is
          generally ahead of /usr/bin in PATH.
          Ah, that answered my next question too! I guess I'll stick with the env
          method, since I like the more general, abstract stuff over the absolute
          anyway.

          Comment

          • Erik Max Francis

            #6
            Re: #!/usr/bin/python or #!/usr/bin/env python?

            John Salerno wrote:
            I understand the difference, but I'm just curious if anyone has any
            strong feelings toward using one over the other? I was reading that a
            disadvantage to the more general usage (i.e. env) is that it finds the
            first python on the path, and that might not be the proper one to use. I
            don't know if that's a real issue most of the time, but it's at least
            something to consider.
            >
            And is Python found in directories other than /usr/bin often enough to
            justify using the more general form?
            Yes. If Python (or indeed any other well-behaving software) is
            installed manually, it will _not_ be installed in /usr/bin. /usr is
            where your primary OS/distribution installation goes. Additional
            software goes elsewhere, such as /usr/local.

            Always prefer to use env over a hardcoded path, because that hardcoded
            path will invariably be wrong. (Yes, for those about to nitpick, it's
            conceivable that env might be somewhere other than /usr/bin. However,
            that is very rare and results in a no-win situations regardless of the
            issue of where Python is installed.)

            --
            Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
            San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
            Never contend with a man who has nothing to lose.
            -- Baltasar Gracian, 1647

            Comment

            • Erik Max Francis

              #7
              Re: #!/usr/bin/python or #!/usr/bin/env python?

              Martin v. Löwis wrote:
              I use /usr/bin/env if I don't know what the operating system is;
              some systems don't have Python in /usr/bin. I use /usr/bin/pythonX.Y
              if I want a specific version on a specific operating system (typically
              Linux).
              Even there, /usr/bin/env pythonX.Y would be a better choice. (Maybe
              that's what you meant.)

              --
              Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
              San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
              Never contend with a man who has nothing to lose.
              -- Baltasar Gracian, 1647

              Comment

              • Martin v. Löwis

                #8
                Re: #!/usr/bin/python or #!/usr/bin/env python?

                Erik Max Francis schrieb:
                >I use /usr/bin/env if I don't know what the operating system is;
                >some systems don't have Python in /usr/bin. I use /usr/bin/pythonX.Y
                >if I want a specific version on a specific operating system (typically
                >Linux).
                >
                Even there, /usr/bin/env pythonX.Y would be a better choice. (Maybe
                that's what you meant.)
                I'm uncertain. When I refer to /usr/bin/python2.3, I know I get the
                system vendor's installation. When I use /usr/bin/env python2.3, I
                may get something that somebody has build and which may not work
                at all, or lack certain add-on packages that the script needs.
                It's bad when the script works for some users but fails for others.

                Regards,
                Martin

                Comment

                • Stephan Kuhagen

                  #9
                  Re: #!/usr/bin/python or #!/usr/bin/env python?

                  Always prefer to use env over a hardcoded path, because that hardcoded
                  path will invariably be wrong. (Yes, for those about to nitpick, it's
                  conceivable that env might be somewhere other than /usr/bin. However,
                  that is very rare and results in a no-win situations regardless of the
                  issue of where Python is installed.)
                  Don't yell at me for bringing in another language, but I really like the
                  trick, Tcl does:
                  #!/bin/sh
                  # The next line starts Tcl \
                  exec tclsh "$0" "$@"
                  This works by the somewhat weird feature of Tcl, that allows comments to be
                  continued in the next line with "\" at the end of the comment-line. It
                  looks unfamiliar, but has several advantages, I think. First it's really
                  VERY unlikely, that there is no /bin/sh (while I found systems with
                  different places for env), and you can add some other features at or before
                  the actual call of the interpreter, i.e. finding the right or preferred
                  version... - This way I coded a complete software-installer, that runs
                  either as a Tcl/Tk-Script with GUI, or as bash-script, when no Tcl is
                  available. - I really would like to have something like that for python,
                  but I did not find a way to achieve that, yet.

                  Regards
                  Stephan

                  Comment

                  • Micha³ Bartoszkiewicz

                    #10
                    Re: #!/usr/bin/python or #!/usr/bin/env python?

                    On 2006-08-09 at 08:02:03 (+0200), Stephan Kuhagen wrote:
                    Don't yell at me for bringing in another language, but I really like the
                    trick, Tcl does:
                    >
                    #!/bin/sh
                    # The next line starts Tcl \
                    exec tclsh "$0" "$@"
                    >
                    This works by the somewhat weird feature of Tcl, that allows comments to be
                    continued in the next line with "\" at the end of the comment-line. It
                    looks unfamiliar, but has several advantages, I think. First it's really
                    VERY unlikely, that there is no /bin/sh (while I found systems with
                    different places for env), and you can add some other features at or before
                    the actual call of the interpreter, i.e. finding the right or preferred
                    version... - This way I coded a complete software-installer, that runs
                    either as a Tcl/Tk-Script with GUI, or as bash-script, when no Tcl is
                    available. - I really would like to have something like that for python,
                    but I did not find a way to achieve that, yet.
                    You could use:
                    #!/bin/sh
                    """exec" python "$0" "$@"""
                    :)

                    --
                    __ _______________ _______________ _______________ _______________ ________
                    | \/ | Micha³ Bartoszkiewicz <embe@magma-net.pl | _ )
                    | |\/| | GG:2298240 | _ \
                    |_| |_|_For all resources, whatever it is, you need more. [RFC1925]_|___/

                    Comment

                    • Stephan Kuhagen

                      #11
                      Re: #!/usr/bin/python or #!/usr/bin/env python?

                      Michał Bartoszkiewicz wrote:
                      #!/bin/sh
                      """exec" python "$0" "$@"""
                      Wow, cool... I like that!

                      Stephan

                      Comment

                      • Erik Max Francis

                        #12
                        Re: #!/usr/bin/python or #!/usr/bin/env python?

                        Stephan Kuhagen wrote:
                        Michał Bartoszkiewicz wrote:
                        >
                        >#!/bin/sh
                        >"""exec" python "$0" "$@"""
                        >
                        Wow, cool... I like that!
                        Only someone genuinely fond of the Tcl hack could ...

                        --
                        Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                        San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
                        No man who needs a monument ever ought to have one.
                        -- Nathaniel Hawthorne

                        Comment

                        • Stephan Kuhagen

                          #13
                          Re: #!/usr/bin/python or #!/usr/bin/env python?

                          Erik Max Francis wrote:
                          >>#!/bin/sh
                          >>"""exec" python "$0" "$@"""
                          >>
                          >Wow, cool... I like that!
                          >
                          Only someone genuinely fond of the Tcl hack could ...
                          True, I admit, I'm a Tcl-Addict... But I really love Python too for many
                          reasons. But I miss features and tricks in both languages that I have in
                          the other...

                          Interleaving the script-language with shell-scripting was one of them. So
                          I'm a little bit happier with Python now... ;-)

                          Stephan

                          Comment

                          • ZeD

                            #14
                            Re: #!/usr/bin/python or #!/usr/bin/env python?

                            Stephan Kuhagen wrote:
                            >#!/bin/sh
                            >"""exec" python "$0" "$@"""
                            >
                            Wow, cool... I like that!
                            yeah, but...

                            $ cat test.py
                            #!/bin/sh
                            """exec" python "$0" "$@"""

                            print "Hello, world"
                            $ file test.py
                            test.py: Bourne shell script text executable

                            --
                            Under construction

                            Comment

                            • Stephan Kuhagen

                              #15
                              Re: #!/usr/bin/python or #!/usr/bin/env python?

                              ZeD wrote:
                              print "Hello, world"
                              $ file test.py
                              test.py: Bourne shell script text executable
                              Yes, the same happens with all Tcl-Scripts. I like to see this as a bug in
                              "file", not in the scripting...

                              Stephan

                              Comment

                              Working...