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

Ben Finney bignose+hates-spam at benfinney.id.au
Thu May 1 23:24:01 EDT 2008


Yves Dorfsman <yves at 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 indistinguishable from a |
  `\                                          feature." —Rich Kulawiec |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list