Cannot pass an argument to python with "#!/usr/bin/env python"

When you use shebang on Linux, the entire rest of the line after the interpreter name is interpreted as a single argument. The python -u gets passed to env as if you'd typed: /usr/bin/env 'python -u'. The /usr/bin/env searches for a binary called python -u, which there isn't one.


This might be a little bit outdated but env(1) manual tells one can use '-S' for that case

#!/usr/bin/env -S python -u

It seems to work pretty good on FreeBSD.


In some environment, env doesn't split arguments. So your env is looking for python -u in your path. We can use sh to work around. Replace your shebang with the following code lines and everything will be fine.

#!/bin/sh
''''exec python -u -- "$0" ${1+"$@"} # '''
# vi: syntax=python

p.s. we need not worry about the path to sh, right?


It is better to use environment variable to enable this. See python doc : http://docs.python.org/2/using/cmdline.html

for your case:

export PYTHONUNBUFFERED=1
script.py