473,770 Members | 5,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#!/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.
http://www.SollerS.ca
Jun 27 '08 #1
48 2744
Yves Dorfsman <yv**@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
Jun 27 '08 #2
In article <87************ @benfinney.id.a u>,
Ben Finney <bi************ ****@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.
Jun 27 '08 #3
On Fri, 02 May 2008 13:24:01 +1000
Ben Finney <bi************ ****@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 <da***@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.
Jun 27 '08 #4
-On [20080502 05:26], Ben Finney (bi************ ****@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...
Jun 27 '08 #5
Yves Dorfsman <yv**@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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #6
Jeroen Ruigrok van der Werven <as*****@in-nomine.orgwrite s:
-On [20080502 05:26], Ben Finney (bi************ ****@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
Jun 27 '08 #7
"D'Arcy J.M. Cain" <da***@druid.ne twrites:
On Fri, 02 May 2008 13:24:01 +1000
Ben Finney <bi************ ****@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
Jun 27 '08 #8
Yves Dorfsman <yv**@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).
Jun 27 '08 #9
-On [20080502 07:51], Ben Finney (bi************ ****@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...
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10053
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.