在 Windows 上运行 python 脚本的正确 shebang

use*_*486 6 python windows windows-10

在 Linux 机器上运行的 Python 脚本有这个 shebang。

#!/usr/bin/python
Run Code Online (Sandbox Code Playgroud)

对于 Windows,在上面使用正确的 shebang 是什么?我使用的是 Windows 10。

Ant*_*hon 2

Linux(和类似 Unix 的机器)上的 shebang 行的解释是由操作系统完成的,而 Windows 不这样做,因此没有适当的 shebang 行。

因此,如果您不想做一些特殊的事情,例如为您的程序选择特定的 Python 版本,您可以省略 shebang(或保留在 Linux 上运行所需的版本)。

如果您的.py文件在 Windows 上注册为由特定的 Python 可执行文件启动,则可以检查该文件的第一行并解释它以确保启动正确的版本。

可以使用原始文件和其他参数以这种方式启动另一个 Python 版本,但请记住,如果您使用它来启动例如 Python 2.7 解释器,其中注册了 Python 3.8,则您的 Python 程序必须有效两个版本的 Python(因此没有print语句、f'{somevar}'字符串等)