#!/usr/bin/python3 shebang 的目的 [英] Purpose of #!/usr/bin/python3 shebang
问题描述
我在几种脚本语言中注意到了这一点,但在本例中,我使用的是 python.在许多教程中,他们会在第一行以 #!/usr/bin/python3
开头.我不明白为什么我们有这个.
I have noticed this in a couple of scripting languages, but in this example, I am using python. In many tutorials, they would start with #!/usr/bin/python3
on the first line. I don't understand why we have this.
- 操作系统不应该知道它是一个 python 脚本(显然它是因为您正在引用它而安装的)
- 如果用户使用的操作系统不是基于 Unix 的会怎样
- 无论出于何种原因,该语言都安装在不同的文件夹中
- 用户的版本不同.特别是当它不是完整版本号时(如 Python3 vs Python32)
如果有的话,由于上面列出的原因,我可以看到这破坏了 python 脚本.
If anything, I could see this breaking the python script because of the listed reasons above.
推荐答案
#!/usr/bin/python3
是一个 shebang 行.
shebang 行定义了解释器所在的位置.在这种情况下,python3
解释器位于 /usr/bin/python3
中.shebang 行也可以是 bash
、ruby
、perl
或任何其他脚本语言的解释器,例如:#!/bin/bash
.
A shebang line defines where the interpreter is located. In this case, the python3
interpreter is located in /usr/bin/python3
. A shebang line could also be a bash
, ruby
, perl
or any other scripting languages' interpreter, for example: #!/bin/bash
.
没有shebang行,操作系统不知道这是一个python脚本,即使你在脚本上设置了执行标志(chmod +x script.py
)并像./script.py
.要使脚本在 python3 中默认运行,请将其调用为 python3 script.py
或设置 shebang 行.
Without the shebang line, the operating system does not know it's a python script, even if you set the execution flag (chmod +x script.py
) on the script and run it like ./script.py
. To make the script run by default in python3, either invoke it as python3 script.py
or set the shebang line.
您可以使用 #!/usr/bin/env python3
在不同系统之间实现可移植性,以防它们在不同位置安装了语言解释器.
You can use #!/usr/bin/env python3
for portability across different systems in case they have the language interpreter installed in different locations.
这篇关于#!/usr/bin/python3 shebang 的目的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!