Shebang Notation:Windows 和 Linux 上的 Python 脚本? [英] Shebang Notation: Python Scripts on Windows and Linux?

查看:27
本文介绍了Shebang Notation:Windows 和 Linux 上的 Python 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些用 Python 编写的小型实用程序脚本,我希望它们可以在 Windows 和 Linux 上使用.我想避免显式调用 Python 解释器.有没有一种简单的方法可以将 shebang 符号指向 Windows 和 Linux 上的正确位置?如果没有,是否有另一种方法允许在 Windows 和 Linux 上隐式调用 Python 解释器,而无需在操作系统之间传输时修改脚本?

I have some small utility scripts written in Python that I want to be usable on both Windows and Linux. I want to avoid having to explicitly invoke the Python interpreter. Is there an easy way to point shebang notation to the correct locations on both Windows and Linux? If not, is there another way to allow implicit invocation of the Python interpreter on both Windows and Linux without having to modify the script when transferring between operating systems?

Cygwin 提供了 Windows 上的 shebang 支持,但我想在 Windows 上使用本机 Windows Python 解释器,而不是 Cygwin 解释器.

The shebang support on Windows is provided Cygwin, but I want to use the native Windows Python interpreter on Windows, not the Cygwin one.

编辑#2:似乎shebang 表示法覆盖了Cygwin 终端中的文件关联.我想我可以卸载 Cygwin Python 并将/usr/bin/python 符号链接到 Windows 原生 Python.

Edit # 2: It appears that shebang notation overrides file associations in Cygwin terminals. I guess I could just uninstall Cygwin Python and symlink /usr/bin/python to Windows-native Python.

推荐答案

阅读 适用于 Windows 的 Python 启动器,最初在 PEP 397.它让您在py.ini"中定义自定义shebang配置(例如使用pypy),开箱即用,您可以使用虚拟shebang,例如#!/usr/bin/env python3,或具有真实路径的shebang,例如#!"C:Python33python.exe".(包含空格的路径需要引用.)您还可以向 shebang 添加命令行选项.例如,下面的shebang添加了脚本终止后进入交互模式的选项:#!/usr/bin/python3 -i.

Read up on the Python Launcher for Windows in the docs, which was initially described in PEP 397. It lets you define custom shebang configurations in "py.ini" (e.g. to use pypy), and out of the box you can use virtual shebangs such as #!/usr/bin/env python3, or shebangs with real paths such as #!"C:Python33python.exe". (Quoting is required for paths containing spaces.) You can also add command-line options to a shebang. For example, the following shebang adds the option to enter interactive mode after the script terminates: #!/usr/bin/python3 -i.

安装程序将 .py(控制台)和 .pyw(GUI)脚本文件类型与分别命名的启动器 py.exe 和 pyw.exe 相关联,以便在 Windows 中启用对脚本的 shebang 支持.对于所有用户安装,启动器安装到 Windows 文件夹(即 %SystemRoot%).对于每用户安装,您可能需要手动将安装目录添加到 PATH 以便在 shell (*) 中使用 py.exe.然后从命令行你可以通过 py -2, py -3, py -2.6, py -3.3- 运行 Python32(32 位),依此类推.当与 -m 结合使用时,启动器很方便,可以使用特定版本的解释器将模块作为脚本运行,例如py -3 -m pip install.

The installer associates .py (console) and .pyw (GUI) script file types with the respectively named launchers, py.exe and pyw.exe, in order to enable shebang support for scripts in Windows. For an all-users installation, the launchers are installed to the Windows folder (i.e. %SystemRoot%). For a per-user installation, you may need to manually add the installation directory to PATH in order to use py.exe in the shell (*). Then from the command line you can run Python via py -2, py -3, py -2.6, py -3.3-32 (32-bit), and so on. The launcher is handy when combined with -m to run a module as a script using a particular version of the interpreter, e.g. py -3 -m pip install.

(*) 3.5+ 中的新安装程序默认为%LocalAppData%ProgramsPythonLauncher",用于按用户安装启动器,而不是安装在python.exe"旁边,它会自动添加这个目录到PATH.

(*) The new installer in 3.5+ defaults to "%LocalAppData%ProgramsPythonLauncher" for a per-user installation of the launcher, instead of installing it beside "python.exe", and it automatically adds this directory to PATH.

这篇关于Shebang Notation:Windows 和 Linux 上的 Python 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆