Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   "#!/usr/bin/env python" vs. "#!/usr/bin/python"? (http://www.velocityreviews.com/forums/t952782-usr-bin-env-python-vs-usr-bin-python.html)

Gilles 09-28-2012 10:32 AM

"#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
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.

Jussi Piitulainen 09-28-2012 10:49 AM

Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
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.

Roy Smith 09-28-2012 10:57 AM

Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
In article <34va6856ocuas7jpueujscf3kdt7k44mfl@4ax.com>,
Gilles <nospam@nospam.com> 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.

Gilles 09-28-2012 11:34 AM

Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
On Fri, 28 Sep 2012 06:57:28 -0400, Roy Smith <roy@panix.com> 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.

D'Arcy Cain 09-28-2012 01:19 PM

Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
On Fri, 28 Sep 2012 06:57:28 -0400
Roy Smith <roy@panix.com> 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 <darcy@druid.net> | 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: darcy@Vex.Net

Gilles 09-28-2012 01:22 PM

Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
On Fri, 28 Sep 2012 09:19:54 -0400, D'Arcy Cain <darcy@druid.net>
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.

Demian Brecht 09-28-2012 01:58 PM

Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
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

Matej Cepl 09-30-2012 09:46 PM

Re: "#!/usr/bin/env python" vs. "#!/usr/bin/python"?
 
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/Featu...seSystemPython),
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



All times are GMT. The time now is 07:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57