Python 源头注释 [英] Python source header comment
问题描述
什么是线
#!/usr/bin/env python
在python脚本的第一行中用于?
in the first line of a python script used for?
推荐答案
在 UNIX 和 Linux 中,这告诉将哪个二进制文件用作解释器(另请参见 维基页面).例如,shell 脚本由 /bin/sh
解释.
In UNIX and Linux this tells which binary to use as an interpreter (see also Wiki page).
For example shell script is interpreted by /bin/sh
.
#!/bin/sh
现在使用 python 有点棘手,因为你不能假设二进制文件安装在哪里,也不能假设你想使用哪个.因此 /usr/bin/env
技巧.它使用 $PATH
中第一个 python 二进制文件.您可以检查执行which python
Now with python it's a bit tricky, because you can't assume where the binary is installed, nor which you want to use. Thus the /usr/bin/env
trick. It's use whichever python binary is first in the $PATH
. You can check that executing which python
使用解释器行,您可以通过将脚本修改为可执行文件来运行脚本.并且只是运行它.因此脚本以
With the interpreter line you can run the script by chmoding it to executable. And just running it. Thus with script beginning with
#!/usr/bin/env python
这两种方法是等价的:
$ python script.py
和(假设您之前已经完成了chmod +x script.py
)
and (assuming that earlier you've done chmod +x script.py
)
$ ./script.py
<小时>
这对于创建系统范围的脚本很有用.
This is useful for creating system wide scripts.
cp yourCmd.py /usr/local/bin/yourCmd
chmod a+rx /usr/local/bin/yourCmd
然后你可以从任何地方调用它
And then you call it from anywhere just with
yourCmd
这篇关于Python 源头注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!