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

Solution 1:

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?

Solution 2:

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

Solution 3:

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.