How should the shebang for a Python script look like?
Some people support #!/usr/bin/env python because it can find the Python interpreter intelligently. Others support #!/usr/bin/python, because now in most GNU/Linux distributions python is the default program.
What are the benefits of the two variants?
The Debian Python Policy states:
Note that Debian/Ubuntu use the alternatives system to manage which version
/usr/bin/pythonactually points to. This has been working very nicely across a lot of python versions at least for me (and I’ve been using python from 2.3 to 2.7 now), with excellent transitions across updates.Note that I’ve never used
pip. I want automatic security upgrades, so I install all my python needs viaaptitude. Using the official Debian/Ubuntu packages keep my system much cleaner than me messing around with the python installation myself.Let me emphasize one thing. The above recommendation refers to system installation of python applications. It makes perfectly sense to have these use the system managed version of python. If you are actually playing around with your own, customized installation of python that is not managed by the operating system, using the
envvariant probably is the correct way of saying “use the user-preferred python”, instead of hard-coding either the system python installation (which would be/usr/bin/python) or any user-custom path.Using
env pythonwill cause your programs to behave differently if you call them from e.g. a python virtualenv.This can be desired (e.g. you are writing a script to work only in your virtualenv). And it can be problematic (you write a tool for you, and expect it to work the same even within a virtualenv – it may suddenly fail because it is missing packages then).