Python源头注释

发布于 2024-07-16 09:12:22 字数 90 浏览 11 评论 0原文

这一行是用来做什么的?

#!/usr/bin/env python

python 脚本的第一行中的

What is the line

#!/usr/bin/env python

in the first line of a python script used for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

抚笙 2024-07-23 09:12:22

在 UNIX 和 Linux 中,这告诉使用哪个二进制文件作为解释器(另请参阅Wiki 页面 )。
例如 shell 脚本由 /bin/sh 解释。

#!/bin/sh

现在使用 python 有点棘手,因为你无法假设二进制文件安装在哪里,也无法假设你想使用哪个。 这就是 /usr/bin/env 技巧。 它使用 $PATH 中第一个的 python 二进制文件。 您可以检查执行 which python

使用解释器行,您可以通过将脚本 chmod 为可执行文件来运行脚本。 只需运行它即可。 因此,以

#!/usr/bin/env python

这两种方法开头的脚本是等效的:

$ python script.py

并且(假设您之前已经完成了chmod +x script.py

$ ./script.py

这对于创建系统范围的脚本很有用。

cp yourCmd.py /usr/local/bin/yourCmd
chmod a+rx /usr/local/bin/yourCmd

然后你可以从任何地方调用它

yourCmd

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

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

these two methods are equivalent:

$ python 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
屋檐 2024-07-23 09:12:22

这称为 shebang 行:

在计算中,shebang(也称为 hashbang、hahpling 或 pound bang)指的是字符“#!” 当它们是文本文件中的前两个字符时。 类 Unix 操作系统将这两个字符的存在视为该文件是脚本的指示,并尝试使用文件中第一行的其余部分指定的解释器来执行该脚本。 例如,Bourne shell 的 shell 脚本以第一行开头:

This is called a shebang line:

In computing, a shebang (also called a hashbang, hashpling, or pound bang) refers to the characters "#!" when they are the first two characters in a text file. Unix-like operating systems take the presence of these two characters as an indication that the file is a script, and try to execute that script using the interpreter specified by the rest of the first line in the file. For instance, shell scripts for the Bourne shell start with the first line:

旧时光的容颜 2024-07-23 09:12:22

在 UNIX 和类似的操作系统下,此行告诉执行文件时将使用哪个解释器。

Under UNIX and similar operating systems, this line tells which interpreter is to be used if the file is executed.

柳若烟 2024-07-23 09:12:22

正如安德里所说。 在 Windows 中,从命令行启动时运行文件的可执行文件依赖于关联:

16:12:40.68 C:\>assoc .py
.py=Python.File

16:13:53.45 C:\>assoc Python.File
Python.File=Python File

16:14:01.70 C:\>ftype Python.File
Python.File="C:\Python30\python.exe" "%1" %*

在 Unix 中,shell 解释器通过打开文件并查看文件中是否有指定的命令来进行推断。

As Andri said. In Windows, the executable to run a file with when launched from the command line relies on an association:

16:12:40.68 C:\>assoc .py
.py=Python.File

16:13:53.45 C:\>assoc Python.File
Python.File=Python File

16:14:01.70 C:\>ftype Python.File
Python.File="C:\Python30\python.exe" "%1" %*

In Unix, the shell interpreter makes the inference by opening the file and seeing if there is a command named in the file.

一刻暧昧 2024-07-23 09:12:22

'/usr/bin/env python' 在 $PATH 中搜索 python 并运行它。

通常env用于设置一些环境变量
对于一个程序

,如果您只是尝试不指定解释器运行该文件,那么该行的作用是告诉您的计算机如何处理该文件。 debian.net/viewtopic.php?t=36789" rel="nofollow noreferrer">更多详细信息

'/usr/bin/env python' searches $PATH for python and runs it.

Usually env is used to set some environment variables
for a program

What that line does is tell your computer what to do with that file, if you simply try to run the file without specifying an interpreter.. more detail

情深如许 2024-07-23 09:12:22

请注意,这一行只不过是对 Windows 中的解释器的注释。

Just a note, this line is nothing more then a comment to the interpreter in Windows.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文