Shebang Notation:Windows 和 Linux 上的 Python 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7574453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:23:06  来源:igfitidea点击:

Shebang Notation: Python Scripts on Windows and Linux?

pythonwindowslinuxshebang

提问by dsimcha

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?

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

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

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

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.

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

回答by Chris J

Not with shebang ... but you might be able to set up a file association, see this SO questionwhich deals with Perl and the associated answers which will also be pertinent as there's known problems with Windows and stdin/out redirection...

不与shebang ...但您可能能够设置文件关联,请参阅此SO question它处理Perl以及相关的答案,因为Windows和stdin / out重定向存在已知问题...

回答by Spencer Rathbun

Unless you are using cygwin, windows has no shebang support. However, when you install python, it add as file association for .py files. If you put just the name of your script on the command line, or double click it in windows explorer, then it will run through python.

除非您使用 cygwin,否则 windows 不支持 shebang。但是,当您安装 python 时,它会添加为 .py 文件的文件关联。如果您只在命令行上输入脚本名称,或者在 Windows 资源管理器中双击它,那么它将通过 python 运行。

What I do is include a #!/usr/bin/env pythonshebang in my scripts. This allows for shebang support on linux. If you run it on a windows machine with python installed, then the file association should be there, and it will run as well.

我所做的是#!/usr/bin/env python在我的脚本中包含一个shebang。这允许在 linux 上支持 shebang。如果你在安装了 python 的 Windows 机器上运行它,那么文件关联应该在那里,它也会运行。

回答by Wai Yip Tung

Install pywin32. One of the nice thing is it setups the file association of *.py to the python interpreter.

安装pywin32。一件好事是它将 *.py 的文件关联设置为 python 解释器。

回答by Eryk Sun

Read up on the Python Launcher for Windowsin 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.

阅读文档中的Python Launcher for Windows,最初在PEP 397 中进行了描述。它允许您在“py.ini”中定义自定义shebang配置(例如使用pypy),并且开箱即用您可以使用虚拟shebang,例如#!/usr/bin/env python3,或具有真实路径的shebang,例如#!"C:\Python33\python.exe"。(包含空格的路径需要引用。)您还可以向 shebang 添加命令行选项。例如,以下shebang添加了在脚本终止后进入交互模式的选项:#!/usr/bin/python3 -i.

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 PATHin 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 -mto run a module as a script using a particular version of the interpreter, e.g. py -3 -m pip install.

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



(*) 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.

(*) 3.5+ 中的新安装程序默认为“%LocalAppData%\Programs\Python\Launcher”,用于按用户安装启动器,而不是安装在“python.exe”旁边,它会自动将此目录添加到PATH.

回答by John Doe

sorry for open old topic.

抱歉打开旧主题。

I create one file py.cmdand place it in the C:\Windows\System32folder

我创建一个文件py.cmd并将其放在C:\Windows\System32文件夹中

py.bat:

py.bat:

@(
@set /p shebang=
)<%1
@set shebang=%shebang:#! =%
@%shebang% %1 %2 %3 %4 %5 %6 %7 %8 %9

py.batfile explain:

py.bat文件说明:

  1. Get the first line from *.pyfile
  2. Remove shebang characters "#! "
  3. Run python file using shebang python path
  1. *.py文件中获取第一行
  2. 删除shebang字符 "#! "
  3. 使用 shebang python 路径运行 python 文件

All windows python script must start with shebang line as the first line in the code:

所有 windows python 脚本都必须以 shebang 行作为代码中的第一行开始:

#! c:\Python27\python.exe

or

或者

#! c:\Python37\python.exe

Then run it: cmd> py SomePyFile.py param1 param1 paramX

然后运行它: cmd> py SomePyFile.py param1 param1 paramX

回答by Brad

Short answer:The easiest way is to install git for windows(git bash) and use shebang lines. #!/usr/bin/env python||anyothercommand

简短回答:最简单的方法是安装git for windows(git bash) 并使用 shebang 行。 #!/usr/bin/env python||anyothercommand

Its the first thing I install on any computer because who doesn't want bash and we all use git right?

这是我在任何计算机上安装的第一件事,因为谁不想要 bash 而我们都使用 git,对吗?

More info:Unlike Cygwin, git bash uses your native windows applications and lets you use bash scripts without any configuration.

更多信息:与 Cygwin 不同,git bash 使用您的本机 Windows 应用程序,并让您无需任何配置即可使用 bash 脚本。

It will automatically treat any file with a shebang line as executable and run the command if its in your path same way bash will on Linux. It also just uses windows ENV variables so anything you add to your path to work from Powershell or cmd will work for git bash too.

它会自动将任何带有 shebang 行的文件视为可执行文件,并运行该命令,如果它在您的路径中与 bash 在 Linux 上的方式相同。它还仅使用 Windows ENV 变量,因此您添加到路径以从 Powershell 或 cmd 工作的任何内容也适用于 git bash。

You can edit env vars in windows by hitting start and typing envshould be the first option. Just edit your user path var (or global one for all users) and add any applications you want available in the command line.

您可以通过点击开始在 Windows 中编辑环境变量,输入env应该是第一个选项。只需编辑您的用户路径 var(或所有用户的全局路径)并添加您希望在命令行中可用的任何应用程序。

Also installs git and hooks it up with windows credentials manager for you and makes it super easy to sign into 2fa enabled svn services like GitHub to bitbucket so you don't have to generate tokens.

还为您安装 git 并将其与 Windows 凭据管理器连接起来,使登录 2fa 启用的 svn 服务(如 GitHub 到 bitbucket)变得非常容易,因此您不必生成令牌。

Also comes with right-click menu to open bash in any location or even a GUI for just using git.

还带有右键单击菜单,可以在任何位置打开 bash,甚至是仅使用 git 的 GUI。

During install, I recommend you tell it to checkout as-is and commit as Unix line endings to be nice to your Linux and macOS buddies. I also recommend many other git configurations but that's another topic.

在安装过程中,我建议您告诉它按原样检出并提交为 Unix 行尾,以便对您的 Linux 和 macOS 伙伴友好。我还推荐了许多其他 git 配置,但这是另一个主题。



Another option:Install WSL(1 not 2) which is even better but requires some configuration. (changing default drive mount paths hooking up credential manager manually etc can ask me about all that if you want there is a lot of tweaks I recommend for running Linux in windows.)

另一种选择:安装 WSL(1 not 2) 更好,但需要一些配置。(更改默认驱动器安装路径,手动连接凭证管理器等可以问我所有这些,如果你想要的话,我建议在 Windows 中运行 Linux 时进行很多调整。)

Using WSL altho more fiddly means you can have Linux versions of all your command line applications separate from your windows ones.

更灵活地使用 WSL 意味着您可以将所有命令行应用程序的 Linux 版本与 Windows 版本分开。

It can still run native windows exe files too if you want but Linux binaries will take precedence if installed.

如果您愿意,它仍然可以运行本机 Windows exe 文件,但如果安装,Linux 二进制文件将优先。

To setup WSL run this command as admin in power shell:

要设置 WSL,请在 Power Shell 中以管理员身份运行此命令:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

*requires reboot

*需要重启

And then search Microsoft app store for Linux and pick a distro. (I recommend Ubuntu 18LTS)

然后在 Microsoft 应用商店中搜索 Linux 并选择一个发行版。(我推荐 Ubuntu 18LTS)

PS - you can also run WSL inside git bash oh and if you happen to use PHP, windows composer.exe will also use shebang lines to run WSL.

PS - 你也可以在 git bash 中运行 WSL 哦,如果你碰巧使用 PHP,windows composer.exe 也会使用 shebang 行来运行 WSL。