#!/ usr / bin / python3的目的

问题:#!/ usr / bin / python3的目的

我已经用几种脚本语言注意到了这一点,但是在此示例中,我使用的是python。在许多教程中,它们将从#!/usr/bin/python3第一行开始。我不明白为什么会有这个。

  • 操作系统不应该知道它是python脚本(显然是安装的,因为您正在引用它)
  • 如果用户使用的不是基于Unix的操作系统怎么办
  • 无论出于何种原因,该语言都安装在其他文件夹中
  • 用户具有不同的版本。特别是当它不是完整版本号时(例如Python3 vs Python32)

如果有的话,由于上面列出的原因,我可以看到这破坏了python脚本。

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.

  • Shouldn’t the operating system know it’s a python script (obviously it’s installed since you are making a reference to it)
  • What if the user is using a operating system that isn’t unix based
  • The language is installed in a different folder for whatever reason
  • The user has a different version. Especially when it’s not a full version number(Like Python3 vs Python32)

If anything, I could see this breaking the python script because of the listed reasons above.


回答 0

#!/usr/bin/python3蛇邦线

shebang线定义了解释器的位置。在这种情况下,python3解释器位于中/usr/bin/python3。甲shebang行也可以是bashrubyperl或任何其他脚本语言解释,例如:#!/bin/bash

没有shebang行,即使您在脚本上设置了执行标志并像那样运行,操作系统也不知道它是python脚本./script.py。要使脚本在python3中默认运行,请以方式调用它python3 script.py或设置shebang行。

#!/usr/bin/env python3如果它们在不同的位置安装了语言解释器,则可以用于跨不同系统的可移植性。

#!/usr/bin/python3 is a shebang line.

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.

Without the shebang line, the operating system does not know it’s a python script, even if you set the execution flag 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.

You can use #!/usr/bin/env python3 for portability across different systems in case they have the language interpreter installed in different locations.


回答 1

这就是所谓的哈希爆炸。如果从外壳程序运行脚本,它将检查第一行以找出应启动哪个程序来解释脚本。

非基于Unix的操作系统将使用其自己的规则来确定如何运行脚本。例如,Windows将使用文件扩展名,并且#将导致第一行被视为注释。

如果Python可执行文件的路径错误,则脚本自然会失败。从标准约定指定的任何位置创建指向实际可执行文件的链接都很容易。

That’s called a hash-bang. If you run the script from the shell, it will inspect the first line to figure out what program should be started to interpret the script.

A non Unix based OS will use its own rules for figuring out how to run the script. Windows for example will use the filename extension and the # will cause the first line to be treated as a comment.

If the path to the Python executable is wrong, then naturally the script will fail. It is easy to create links to the actual executable from whatever location is specified by standard convention.


回答 2

此行有助于找到将运行脚本的程序可执行文件。这种shebang表示法在大多数脚本语言中都是非常标准的(至少在成熟的操作系统上使用)。

该行的重要方面是指定将使用哪个解释器。例如,在许多以开发为中心的Linux发行版中,通常同​​时安装多个版本的python是正常的。

Python 2.x和Python 3不是100%兼容的,因此这种差异可能非常重要。因此,#! /usr/bin/python#! /usr/bin/python3并不相同(也与#! /usr/bin/env python3本页面其他地方所述的不完全相同)。

This line helps find the program executable that will run the script. This shebang notation is fairly standard across most scripting languages (at least as used on grown-up operating systems).

An important aspect of this line is specifying which interpreter will be used. On many development-centered Linux distributions, for example, it is normal to have several versions of python installed at the same time.

Python 2.x and Python 3 are not 100% compatible, so this difference can be very important. So #! /usr/bin/python and #! /usr/bin/python3 are not the same (and neither are quite the same as #! /usr/bin/env python3 as noted elsewhere on this page.


回答 3

  1. 这条线是怎么回事

  2. 它被忽略。

  3. 它将无法运行,应更改为指向正确的位置。还是env应该使用。

  4. 它将无法运行,并且可能无法在其他版本下运行。

  1. And this line is how.

  2. It is ignored.

  3. It will fail to run, and should be changed to point to the proper location. Or env should be used.

  4. It will fail to run, and probably fail to run under a different version regardless.


回答 4

为了阐明shebang行在Windows中的工作原理,请参阅3.7 Python文档

  • 如果脚本文件的第一行以#!开头,则称为“ shebang”行。Linux和其他类似Unix的操作系统对此行具有本地支持,它们通常在此类系统上用于指示应如何执行脚本。
  • Windows的Python启动器允许Windows上的Python脚本使用相同的功能
  • 为了允许Python脚本中的shebang行在Unix和Windows之间可移植,启动器支持许多“虚拟”命令来指定要使用的解释器。支持的虚拟命令是:
    • / usr / bin / env python
      • shebang行的/ usr / bin / env格式还有另一个特殊属性。在查找已安装的Python解释器之前,此表单将在可执行文件PATH中搜索Python可执行文件。这对应于Unix env程序的行为,该程序执行PATH搜索。
    • / usr / bin / python
    • / usr / local / bin / python
    • Python

To clarify how the shebang line works for windows, from the 3.7 Python doc:

  • If the first line of a script file starts with #!, it is known as a “shebang” line. Linux and other Unix like operating systems have native support for such lines and they are commonly used on such systems to indicate how a script should be executed.
  • The Python Launcher for Windows allows the same facilities to be used with Python scripts on Windows
  • To allow shebang lines in Python scripts to be portable between Unix and Windows, the launcher supports a number of ‘virtual’ commands to specify which interpreter to use. The supported virtual commands are:
    • /usr/bin/env python
      • The /usr/bin/env form of shebang line has one further special property. Before looking for installed Python interpreters, this form will search the executable PATH for a Python executable. This corresponds to the behaviour of the Unix env program, which performs a PATH search.
    • /usr/bin/python
    • /usr/local/bin/python
    • python

回答 5

实际上,确定文件的文件类型非常复杂,因此现在操作系统不能仅仅知道。它可以基于-做出很多猜测

  • 延期
  • 尿路感染
  • 哑剧

但是命令行并不打扰所有这一切,因为它在有限的向后兼容层上运行,从那一刻起,废话什么都没有。如果您双击确定,现代操作系统可以解决这一问题,但是如果您从终端运行它,则不会,因为终端并不关心您的操作系统特定的文件键入API。

关于其他要点。这很方便,也可以运行

python3 path/to/your/script

如果您的python不在指定的路径中,则它将无法正常工作,但是我们倾向于安装一些东西来使类似的工作正常进行,而不是相反。您是否在* nix下实际上并不重要,是否考虑这行取决于您的shell,因为这是一个shellcode。因此,例如,您可以bash在Windows下运行。

实际上,您可以完全省略此行,这仅意味着调用方将必须指定解释器。另外,请勿将口译员放在非标准位置,然后尝试在不提供口译员的情况下调用脚本。

Actually the determination of what type of file a file is very complicated, so now the operating system can’t just know. It can make lots of guesses based on –

  • extension
  • UTI
  • MIME

But the command line doesn’t bother with all that, because it runs on a limited backwards compatible layer, from when that fancy nonsense didn’t mean anything. If you double click it sure, a modern OS can figure that out- but if you run it from a terminal then no, because the terminal doesn’t care about your fancy OS specific file typing APIs.

Regarding the other points. It’s a convenience, it’s similarly possible to run

python3 path/to/your/script

If your python isn’t in the path specified, then it won’t work, but we tend to install things to make stuff like this work, not the other way around. It doesn’t actually matter if you’re under *nix, it’s up to your shell whether to consider this line because it’s a shellcode. So for example you can run bash under Windows.

You can actually ommit this line entirely, it just mean the caller will have to specify an interpreter. Also don’t put your interpreters in nonstandard locations and then try to call scripts without providing an interpreter.