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

I needed to have a directly executable python script, so i started the file with #!/usr/bin/env python . However, I also need unbuffered output, so i tried #!/usr/bin/env python -u , but that fails with python -u: no such file or directory .

I found out that #/usr/bin/python -u works, but I need it to get the python in PATH to support virtual env environments.

What are my options?


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

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

ps we need not worry about the path to sh, right?


Passing arguments to the shebang line is not standard and in as you have experimented do not work in combination with env in Linux. The solution with bash is to use the builtin command "set" to set the required options. I think you can do the same to set unbuffered output of stdin with a python command.

my2c

链接地址: http://www.djcxy.com/p/46406.html

上一篇: 如何杀死所有匹配名称的进程?

下一篇: “#!/ usr / bin / env python”无法将参数传递给python