Why it is recommended to use /usr/bin/env python as opposed to /usr/bin/python. This was inspired from reading the responses here: What do I use on linux to make a python program executable
I think both would do exactly the same thing, but the answer that mentions both equally has 2 votes, rather than 88 votes for the answer that says to use env
. People seem to really agree with using env, but don't explain why. Does env help you find any executable named "python", rather than just the one in a specific path which the user could have customized?
I'm really interested to know the various advantages and use cases for using env, and man env
didn't explain it. man env
only says that you can use env to set environment variables before running, NAME=VALUE, but we aren't doing that in the linked example.
I feel the community clearly is recommending to use env, but my own research has not hinted at a reason, so if you have ideas why it's good or good style to do so, please answer.
Using #!/usr/bin/env python
ensures that the first Python interpreter in the currently-defined PATH is used, honoring any user overrides (which, for Python, includes virtualenv
s and the like).
This is particularly important for environments such as MacOS, where OS-vendor packages are often out-of-date (for instance, /bin/bash
is 3.2 -- a release series from 2006 -- whereas if the user has MacPorts or Homebrew, /opt/local/bin/bash
or /usr/local/bin/bash
may be a modern 4.x; thus, #!/usr/bin/env bash
will gain the benefit of any updated bash interpreter a user has installed, whether system-wide or under their home directory, so long as the location of that interpreter is present in their PATH at runtime).