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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yves Dorfsman

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

    On UNIX, some people use
    #!/usr/bin/env python

    While other use
    #!/usr/bin/python

    Why is one preferred over the other one ?

    Thanks.

    --
    Yves.
    Calgary AIX Linux UNIX React TypeScript JavaScript python contractor consultant programmer Yves Dorfsman

  • Ben Finney

    #2
    Re: #!/usr/bin/env python vs. #!/usr/bin/python

    Yves Dorfsman <yves@zioup.com writes:
    On UNIX, some people use
    #!/usr/bin/env python
    >
    While other use
    #!/usr/bin/python
    You haven't indicated your understanding of what the difference in
    meaning is, so I'll explain it for those who might not know.

    The shebang line (the initial line of the file beginning with "#!")
    takes advantage of OS kernels that determine how to execute a file
    based on the first few bytes of the file. The shebang line tells the
    kernel that this file should be executed by passing it as input to
    a process started by another command.

    The specified command takes the form of a fully-qualified file path,
    and zero or one arguments to the program. That command is then
    executed by the kernel, and the Python program file is passed as input
    to the resulting process.

    The difference between the two is thus what command is executed to
    interpret the Python program.

    * "#! /usr/bin/env python" will run the command "/usr/bin/env python".
    The 'env(1)' manual page says its purpose is to "run a program in a
    modified environment", but it also has the effect that the command
    is searched on the current PATH variable, and executed based on the
    first occurrence.

    * "#! /usr/bin/python" will run the command "/usr/bin/python", which
    is of course the system Python instance as installed by most OS
    packaging systems. That command is run, and the result is the Python
    interpreter.
    Why is one preferred over the other one ?
    I've never clearly understood why people want to use "#! /usr/bin/env
    python", which is prone to finding a different Python from the one
    installed by the operating system. I'd be interested to see what
    responses are in favour of it, and what the reasoning is.

    One possible reason is that the programmer is attempting to allow for
    systems where Python has been installed, but not from an operating
    system package.

    I much prefer "#! /usr/bin/python" because I want my Python programs
    to, by default, be run with the default Python, and depend on Python
    being installed by the operating system's package manager. On systems
    that use shebang lines and that actually have standardised filesystem
    locations, the default Python is found at '/usr/bin/python'.

    --
    \ "Any sufficiently advanced bug is indistinguishab le from a |
    `\ feature." —Rich Kulawiec |
    _o__) |
    Ben Finney

    Comment

    • Roy Smith

      #3
      Re: #!/usr/bin/env python vs. #!/usr/bin/python

      In article <87abj91j8u.fsf @benfinney.id.a u>,
      Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
      I've never clearly understood why people want to use "#! /usr/bin/env
      python", which is prone to finding a different Python from the one
      installed by the operating system. I'd be interested to see what
      responses are in favour of it, and what the reasoning is.
      >
      One possible reason is that the programmer is attempting to allow for
      systems where Python has been installed, but not from an operating
      system package.
      You've got it exactly.

      I'm currently using Python to write unit tests as part of a build system.
      Many of our development boxes don't have python installed in /usr/bin (or
      perhaps at all). And even if they did, we might want to use a different
      version of Python on different branches of the code.

      We've got Python built for all our platforms and the binaries stored in our
      source control system. When you check out a particular branch, you get the
      right version of Python for that branch. By having the Python scripts
      start with #!/usr/bin/env python, I can select the version of Python I want
      just by changing the environment.

      Then, of course, I recently ran across a machine where env was installed in
      /opt/gnu/bin instead of /usr/bin. Sigh. Sometimes you just can't win.

      Comment

      • D'Arcy J.M. Cain

        #4
        Re: #!/usr/bin/env python vs. #!/usr/bin/python

        On Fri, 02 May 2008 13:24:01 +1000
        Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
        I much prefer "#! /usr/bin/python" because I want my Python programs
        to, by default, be run with the default Python, and depend on Python
        being installed by the operating system's package manager. On systems
        that use shebang lines and that actually have standardised filesystem
        locations, the default Python is found at '/usr/bin/python'.
        You have lived a sheltered life. Not every packaging system puts the
        executible in /usr/bin. Many systems use /usr/local/bin. NetBSD
        uses /usr/pkg/bin but allows you to define your own pkg root.
        Using /usr/bin/env allows your code to run on all these systems.

        --
        D'Arcy J.M. Cain <darcy@druid.ne t | Democracy is three wolves
        http://www.druid.net/darcy/ | and a sheep voting on
        +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

        Comment

        • Jeroen Ruigrok van der Werven

          #5
          Re: #!/usr/bin/env python vs. #!/usr/bin/python

          -On [20080502 05:26], Ben Finney (bignose+hates-spam@benfinney. id.au) wrote:
          >I've never clearly understood why people want to use "#! /usr/bin/env
          >python", which is prone to finding a different Python from the one
          >installed by the operating system. I'd be interested to see what
          >responses are in favour of it, and what the reasoning is.
          Simple, some systems are not as peculiar as a lot of Linux boxes which
          chug everything into /usr/bin, which is OS territory (as has been decreed
          long ago by hier(7)), but rather use /usr/local/bin (all BSD Unix and
          derivatives) or /opt or whatever convention a particular operating system
          has.

          And prone to find the wrong Python, it all depends upon proper $PATH
          administration.

          As such, your script with #!/usr/bin/python is as bad as an ash shell script
          with #!/bin/bash. #!/usr/bin/env python is more cross-OS friendly, there's
          more than just Linux you know.

          --
          Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org/ asmodai
          イェルーン ラウフロッ ク ヴァン デル ウェルヴェ ン
          http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
          I dream of Love as Time runs through my hand...

          Comment

          • Tim Roberts

            #6
            Re: #!/usr/bin/env python vs. #!/usr/bin/python

            Yves Dorfsman <yves@zioup.com wrote:
            >
            >On UNIX, some people use
            >#!/usr/bin/env python
            >
            >While other use
            >#!/usr/bin/python
            >
            >Why is one preferred over the other one ?
            The /usr/bin/env solution finds the Python interpreter anywhere on the
            PATH, whether it be /usr/bin or /usr/local/bin, or whatever. With
            /usr/bin/python, it MUST be in /usr/bin.

            Way back when, Python wasn't included in Linux distributions by default, so
            it was difficult to predict where it would be. /usr/bin/env, on the other
            hand, is well-established at that location.

            These days, since Python is nearly ubiquitous, I suspect it is not so
            important.
            --
            Tim Roberts, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            • Ben Finney

              #7
              Re: #!/usr/bin/env python vs. #!/usr/bin/python

              Jeroen Ruigrok van der Werven <asmodai@in-nomine.orgwrite s:
              -On [20080502 05:26], Ben Finney (bignose+hates-spam@benfinney. id.au) wrote:
              I've never clearly understood why people want to use "#!
              /usr/bin/env python", which is prone to finding a different Python
              from the one installed by the operating system. I'd be interested
              to see what responses are in favour of it, and what the reasoning
              is.
              >
              Simple, some systems are not as peculiar as a lot of Linux boxes
              which chug everything into /usr/bin, which is OS territory (as has
              been decreed long ago by hier(7)), but rather use /usr/local/bin
              (all BSD Unix and derivatives) or /opt or whatever convention a
              particular operating system has.
              To my mind, the Python interpreter installed by a package as
              distributed with the OS *is* OS territory and belongs in /usr/bin/.
              As such, your script with #!/usr/bin/python is as bad as an ash
              shell script with #!/bin/bash.
              Clearly if the program is written to be interpreted by the Ash shell,
              it should not declare Bash as the interpreter.

              I don't see how declaring Python as the interpreter for a Python
              program is supposed to be "as bad" as that.

              --
              \ "Don't be afraid of missing opportunities. Behind every failure |
              `\ is an opportunity somebody wishes they had missed." -- Jane |
              _o__) Wagner, via Lily Tomlin |
              Ben Finney

              Comment

              • Ben Finney

                #8
                Re: #!/usr/bin/env python vs. #!/usr/bin/python

                "D'Arcy J.M. Cain" <darcy@druid.ne twrites:
                On Fri, 02 May 2008 13:24:01 +1000
                Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                I much prefer "#! /usr/bin/python" because I want my Python
                programs to, by default, be run with the default Python, and
                depend on Python being installed by the operating system's package
                manager. On systems that use shebang lines and that actually have
                standardised filesystem locations, the default Python is found at
                '/usr/bin/python'.
                >
                You have lived a sheltered life. Not every packaging system puts the
                executible in /usr/bin. Many systems use /usr/local/bin.
                They use that for the operating-system-installed default Python
                interpreter? Colour me incredulous.

                --
                \ “[The RIAA] have the patience to keep stomping. They’re |
                `\ playing whack-a-mole with an infinite supply of tokens.” |
                _o__) —kennon, http://kuro5hin.org/ |
                Ben Finney

                Comment

                • Duncan Booth

                  #9
                  Re: #!/usr/bin/env python vs. #!/usr/bin/python

                  Yves Dorfsman <yves@zioup.com wrote:
                  On UNIX, some people use
                  #!/usr/bin/env python
                  >
                  While other use
                  #!/usr/bin/python
                  >
                  Why is one preferred over the other one ?
                  >
                  I don't think the answers so far have communicated what I believe to be the
                  important point: it isn't that one is always better than the other, it
                  depends on what you are trying to achieve.

                  The first one runs the Python found from the environment. This means you
                  can write a script and expect it to run on systems configured differently.
                  You might prefer in some cases to specify a particular version of Python:

                  #!/usr/bin/env python2.5

                  The second one runs a specific copy of Python (and here it is even more
                  likely that you'll want to specify a particular version). This is important
                  if your program is being run as a service or some other background
                  situation where the environment isn't set up. For example Subversion hooks
                  all run with an empty environment, and cron jobs run with a default
                  environment which may not include python (e.g. if it is in /usr/local/bin).


                  Comment

                  • Jeroen Ruigrok van der Werven

                    #10
                    Re: #!/usr/bin/env python vs. #!/usr/bin/python

                    -On [20080502 07:51], Ben Finney (bignose+hates-spam@benfinney. id.au) wrote:
                    >To my mind, the Python interpreter installed by a package as
                    >distributed with the OS *is* OS territory and belongs in /usr/bin/.
                    That's the difference with a distribution, such as Linux, and full OSes ,
                    such as BSDs or commercial Unix variants. They prefer to keep a pristine
                    state for the OS vendor files versus what the user can opt to install
                    himself, hence the /usr/bin - /usr/local/bin separation. Same for sbin, lib,
                    and so on. It effectively guarantees you can nuke /usr/local without ill
                    consequences for your OS.

                    Different philosophies, but after having spent more than 10+ years on too
                    many Unix and Unix-like systems I know the importance of platform
                    portability a bit too much and hardcoding a shebang sequence is not the
                    solution in general. Using env is the, arguably, best solution available.

                    --
                    Jeroen Ruigrok van der Werven <asmodai(-at-)in-nomine.org/ asmodai
                    イェルーン ラウフロッ ク ヴァン デル ウェルヴェ ン
                    http://www.in-nomine.org/ | http://www.rangaku.org/ | GPG: 2EAC625B
                    Felix, qui potuit rerum cognoscere causas...

                    Comment

                    • Ben Finney

                      #11
                      Re: #!/usr/bin/env python vs. #!/usr/bin/python

                      Jeroen Ruigrok van der Werven <asmodai@in-nomine.orgwrite s:
                      -On [20080502 07:51], Ben Finney (bignose+hates-spam@benfinney. id.au) wrote:
                      To my mind, the Python interpreter installed by a package as
                      distributed with the OS *is* OS territory and belongs in /usr/bin/.
                      >
                      That's the difference with a distribution, such as Linux, and full
                      OSes , such as BSDs or commercial Unix variants. They prefer to keep
                      a pristine state for the OS vendor files versus what the user can
                      opt to install himself, hence the /usr/bin - /usr/local/bin
                      separation.
                      Fine so far. /usr/local/ is certainly for "what the (system
                      administrator) user opts to install themselves".
                      It effectively guarantees you can nuke /usr/local without ill
                      consequences for your OS.
                      You say this as though it's a property that a GNU/Linux distribution
                      doesn't have. But the "keep /usr/local/ untouched by OS packages"
                      approach taken by GNU/Linux *also* means that /usr/local/ can be blown
                      away without ill consequences for the OS. So I don't see why you draw
                      that distinction here.

                      The difference seems to be that Python is an OS-installable package on
                      GNU/Linux, and thus gets installed to the OS-packaged location. So the
                      default Python installation should work.

                      Whereas if Python is *not* installed from an OS package, it's up to
                      the sys admin to ensure that it works -- not up to my program. So I
                      don't see the point in making it work by default, when what I want for
                      my program is that it works *with the default Python*, not with some
                      non-default installation.

                      --
                      \ "Truth is stranger than fiction, but it is because fiction is |
                      `\ obliged to stick to possibilities, truth isn't." -- Mark |
                      _o__) Twain, _Following the Equator_ |
                      Ben Finney

                      Comment

                      • D'Arcy J.M. Cain

                        #12
                        Re: #!/usr/bin/env python vs. #!/usr/bin/python

                        On Fri, 02 May 2008 15:50:22 +1000
                        Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                        You have lived a sheltered life. Not every packaging system puts the
                        executible in /usr/bin. Many systems use /usr/local/bin.
                        >
                        They use that for the operating-system-installed default Python
                        interpreter? Colour me incredulous.
                        OK, let me get out my crayons. However, note that I did not say
                        "operating-system-installed." I said a packaging system puts it
                        there. In fact, the NetBSD packaging system works on many systems
                        including Linux and thus is not an operating system packager.

                        --
                        D'Arcy J.M. Cain <darcy@druid.ne t | Democracy is three wolves
                        http://www.druid.net/darcy/ | and a sheep voting on
                        +1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.

                        Comment

                        • Ben Finney

                          #13
                          Re: #!/usr/bin/env python vs. #!/usr/bin/python

                          "D'Arcy J.M. Cain" <darcy@druid.ne twrites:
                          On Fri, 02 May 2008 13:24:01 +1000
                          Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                          I much prefer "#! /usr/bin/python" because I want my Python
                          programs to, by default, be run with the default Python, and
                          depend on Python being installed by the operating system's package
                          manager. On systems that use shebang lines and that actually have
                          standardised filesystem locations, the default Python is found at
                          '/usr/bin/python'.
                          >
                          You have lived a sheltered life. Not every packaging system puts the
                          executible in /usr/bin. Many systems use /usr/local/bin.
                          "D'Arcy J.M. Cain" <darcy@druid.ne twrites:
                          On Fri, 02 May 2008 15:50:22 +1000
                          Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                          They use that for the operating-system-installed default Python
                          interpreter? Colour me incredulous.
                          >
                          OK, let me get out my crayons. However, note that I did not say
                          "operating-system-installed."
                          That is, however, the context I've been explicitly using since this
                          sub-thread began.

                          The OP was asking why people prefer on over the other. My answer is
                          that I prefer specifying "give me the default OS Python" because
                          anything not installed by the OS is to non-standardised for me to
                          worry about.

                          Others may prefer something different, but then they get to wear
                          whatever problems occur as a result of that choice. I continue to be
                          bemused by that preference, and nothing that I've seen so far in this
                          thread illuminates the issue more.

                          --
                          \ "Nothing so needs reforming as other people's habits." -- Mark |
                          `\ Twain, _Pudd'n'head Wilson_ |
                          _o__) |
                          Ben Finney

                          Comment

                          • Thorsten Kampe

                            #14
                            Re: #!/usr/bin/env python vs. #!/usr/bin/python

                            * Ben Finney (Fri, 02 May 2008 23:30:01 +1000)
                            The OP was asking why people prefer on over the other. My answer is
                            that I prefer specifying "give me the default OS Python" because
                            anything not installed by the OS is to non-standardised for me to
                            worry about.
                            >
                            Others may prefer something different, but then they get to wear
                            whatever problems occur as a result of that choice. I continue to be
                            bemused by that preference, and nothing that I've seen so far in this
                            thread illuminates the issue more.
                            You're missing the point. Apart from the really dubious terms you use
                            ("OS installable package"), using env in the first line has exactly the
                            effect to use the default path of Python (which is the first entry in
                            your path) and not some hard-coded path (which might not even exist).

                            Thorsten

                            Comment

                            • Roy Smith

                              #15
                              Re: #!/usr/bin/env python vs. #!/usr/bin/python

                              In article <87r6ckzvyv.fsf @benfinney.id.a u>,
                              Ben Finney <bignose+hate s-spam@benfinney. id.auwrote:
                              Whereas if Python is *not* installed from an OS package, it's up to
                              the sys admin to ensure that it works -- not up to my program. So I
                              don't see the point in making it work by default, when what I want for
                              my program is that it works *with the default Python*, not with some
                              non-default installation.
                              Ben,

                              Have you ever shipped software to a customer? Imagine the following
                              conversation:

                              Customer: "Your product is broken. It says it can't find python, and I
                              know I have it installed".

                              Vendor: "Where do you have it installed?"

                              Customer: "In /opt/bin/python"

                              Vendor: "Oh, that's your problem, it HAS to be in /usr/bin/python".

                              Customer: "I can't install it there because <insert whatever silly reason
                              the customer has>. If you can't make your product work without requiring
                              me to install python in /usr/bin, I'm afraid I can't buy your product".

                              Vendor: "No problem sir, I'll be happy to tell our sales folks to stop
                              bothering you".

                              If you want to hard-code /usr/bin/python into your application, that's your
                              decision. If you would like to take on the task of convincing every
                              sysadmin in the world to do things the way you think they should be done,
                              have fun.

                              Comment

                              Working...