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

查看:136
本文介绍了Shebang注释: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:\Python33\python.exe"). (对于包含空格的路径,必须加引号.)您还可以将命令行选项添加到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:\Python33\python.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,以便在外壳程序(*)中使用py.exe.然后,可以从命令行通过py -2py -3py -2.6py -3.3-32(32位)等运行Python.当与-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%\ Programs \ Python \ Launcher",而不是将其安装在"python.exe"旁边,并自动添加此目录到PATH.

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

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

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