是否可以在python shebang中包含命令行选项? [英] Is it possible to include command line options in the python shebang?

查看:63
本文介绍了是否可以在python shebang中包含命令行选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python脚本的顶部有规范的shebang。

I have the canonical shebang at the top of my python scripts.

#!/usr/bin/env python

但是,我仍然经常想在运行脚本时将未缓冲的输出导出到日志文件中,所以我最终调用:

However, I still often want to export unbuffered output to a log file when I run my scripts, so I end up calling:

$ python -u myscript.py &> myscript.out &

我可以像这样...将-u选项嵌入到shebang中吗?

Can I embed the -u option in the shebang like so...

#!/usr/bin/env python -u

并且只能调用:

$ ./myscript.py &> myscript.out &

...仍然能够获得缓冲?我怀疑这行不通,并想在尝试之前进行检查。有什么事情可以做到这一点?

...to still get the unbuffering? I suspect that won't work, and want to check before trying. Is there something that would accomplish this?

推荐答案

在shebang行上可以有参数,但是大多数操作系统的参数很小限制参数数量。 POSIX仅要求支持一个参数,这在包括Linux在内都是常见的。

You can have arguments on the shebang line, but most operating systems have a very small limit on the number of arguments. POSIX only requires that one argument be supported, and this is common, including Linux.

由于您使用的是 / usr / bin / env 命令,您已经在 python 中用完了一个参数,因此不能添加另一个参数 -u 。如果要使用 python -u ,则需要将绝对路径硬编码为 python 而不是使用 / usr / bin / env ,例如

Since you're using the /usr/bin/env command, you're already using up that one argument with python, so you can't add another argument -u. If you want to use python -u, you'll need to hard-code the absolute path to python instead of using /usr/bin/env, e.g.

#!/usr/bin/python -u

请参阅以下相关问题:如何在shebang中使用多个参数(例如#!)?

See this related question: How to use multiple arguments with a shebang (i.e. #!)?

这篇关于是否可以在python shebang中包含命令行选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆