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

Discussion in 'Python' started by Gilles, Sep 28, 2012.

  1. Gilles

    Gilles Guest

    Hello

    I've seen both shebang lines to run a Python script on a *nix host:

    #!/usr/bin/env python
    #!/usr/bin/python

    What's the difference?

    Thank you.
     
    Gilles, Sep 28, 2012
    #1
    1. Advertising

  2. Gilles writes:

    > #!/usr/bin/env python
    > #!/usr/bin/python
    >
    > What's the difference?


    Not much if your python is /usr/bin/python: env looks for python and
    finds the same executable.

    When python is not /usr/bin/python but something else that is still
    found by your system, /usr/bin/env still finds it.

    For example, in a server where I work, python3 is installed as
    something like /opt/python/3.2.2-gcc/bin/python3. There is no
    /usr/bin/python3 at all, but "#! /usr/bin/env python3" works.
     
    Jussi Piitulainen, Sep 28, 2012
    #2
    1. Advertising

  3. Gilles

    Roy Smith Guest

    In article <>,
    Gilles <> wrote:

    > Hello
    >
    > I've seen both shebang lines to run a Python script on a *nix host:
    >
    > #!/usr/bin/env python
    > #!/usr/bin/python
    >
    > What's the difference?


    The first one looks through your PATH to find the right python
    interpreter to run. The second one is hard-wired to run /usr/bin/python.

    If you only have a single copy of python installed, it doesn't really
    matter which you use. But, you might as well get into the habit of
    using the /usr/bin/env flavor because it's more flexible.

    I'm working on a number of different python projects. For each one, I
    set up a new virtual environment using virtualenv. This lets me run
    different versions of python in different projects, with different
    collections of installed packages (and possibly different versions). I
    can only do this because I use the /usr/bin/env line in all my scripts.
     
    Roy Smith, Sep 28, 2012
    #3
  4. Gilles

    Gilles Guest

    On Fri, 28 Sep 2012 06:57:28 -0400, Roy Smith <> wrote:
    >The first one looks through your PATH to find the right python
    >interpreter to run. The second one is hard-wired to run /usr/bin/python.
    >
    >If you only have a single copy of python installed, it doesn't really
    >matter which you use. But, you might as well get into the habit of
    >using the /usr/bin/env flavor because it's more flexible.


    Thanks guys. I suspected that's what the difference was.
     
    Gilles, Sep 28, 2012
    #4
  5. Gilles

    D'Arcy Cain Guest

    On Fri, 28 Sep 2012 06:57:28 -0400
    Roy Smith <> wrote:
    > > I've seen both shebang lines to run a Python script on a *nix host:
    > >
    > > #!/usr/bin/env python
    > > #!/usr/bin/python
    > >
    > > What's the difference?

    >
    > The first one looks through your PATH to find the right python
    > interpreter to run. The second one is hard-wired to run /usr/bin/python.
    >
    > If you only have a single copy of python installed, it doesn't really
    > matter which you use. But, you might as well get into the habit of
    > using the /usr/bin/env flavor because it's more flexible.


    Not just flexible but portable. On various systems I have Python
    in /usr/bin, /usr/local/bin and /usr/pkg/bin. "#!/usr/bin/env python"
    finds it in each case so I only need one version of the script.

    --
    D'Arcy J.M. Cain <> | 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.
    IM:
     
    D'Arcy Cain, Sep 28, 2012
    #5
  6. Gilles

    Gilles Guest

    On Fri, 28 Sep 2012 09:19:54 -0400, D'Arcy Cain <>
    wrote:
    >Not just flexible but portable. On various systems I have Python
    >in /usr/bin, /usr/local/bin and /usr/pkg/bin. "#!/usr/bin/env python"
    >finds it in each case so I only need one version of the script.


    Good to know.
     
    Gilles, Sep 28, 2012
    #6
  7. On 12-09-28 06:19 AM, D'Arcy Cain wrote:
    > Not just flexible but portable. On various systems I have Python
    > in /usr/bin, /usr/local/bin and /usr/pkg/bin. "#!/usr/bin/env python"
    > finds it in each case so I only need one version of the script.
    >


    +1. This also resolves correctly on Cygwin, even if Python is installed
    via Windows installers (as long as it's on system PATH). Tremendously
    useful if you're bouncing between *nix and Windows regularly.

    --
    Demian Brecht
    @demianbrecht
    http://demianbrecht.github.com
     
    Demian Brecht, Sep 28, 2012
    #7
  8. Gilles

    Matej Cepl Guest

    On 28/09/12 12:57, Roy Smith wrote:
    > But, you might as well get into the habit of
    > using the /usr/bin/env flavor because it's more flexible.


    In the same manner as one's freedom-fighter is another's fundamentalist
    terrorist, what's flexible could be also dangerous. E.g.,

    #!/usr/bin/env python

    is forbidden in the core Fedora packages
    (https://fedoraproject.org/wiki/Features/SystemPythonExecutablesUseSystemPython),
    because nobody is willing to risk that by some random python binary in
    /usr/local/bin some core infrastructure of Fedora installations (e.g.,
    yum) could be broken.

    Matìj
     
    Matej Cepl, Sep 30, 2012
    #8
    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,626
    Erik Max Francis
    Nov 10, 2003
  2. Replies:
    6
    Views:
    561
    Jorgen Grahn
    Oct 6, 2005
  3. Adriano Ferreira
    Replies:
    9
    Views:
    580
    Mike Meyer
    Dec 3, 2005
  4. John Salerno
    Replies:
    30
    Views:
    1,968
    Stephan Kuhagen
    Aug 10, 2006
  5. Yves Dorfsman

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

    Yves Dorfsman, May 2, 2008, in forum: Python
    Replies:
    27
    Views:
    2,007
    Tim Roberts
    May 10, 2008
Loading...

Share This Page