Shebang 表示法: Python 脚本在 Windows 和 Linux 上的应用?

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

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

编辑 # 2: 似乎 shebang 符号覆盖了 Cygwin 终端中的文件关联。我想我可以直接卸载 Cygwin Python 和 symlink/usr/bin/Python 到 Windows 本机 Python。

99630 次浏览

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

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.

What I do is include a #!/usr/bin/env python shebang 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.

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

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.

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.


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

sorry for open old topic.

I create one file py.cmd and place it in the C:\Windows\System32 folder

py.bat:

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

py.bat file explain:

  1. Get the first line from *.py file
  2. Remove shebang characters "#! "
  3. Run python file using shebang python path

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

#! c:\Python27\python.exe

or

#! c:\Python37\python.exe

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

Short answer: The easiest way is to install git for windows wich comes with GitBash. Then add shebang lines in your scripts to indicate they should be run with python when executed. #!/usr/bin/env python

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

It will automatically treat any file with a shebang line as executable the same way Linux shells do. eg: #!/usr/bin/env php or #!/usr/bin/env node or any other application you want will work as long as you add the paths to your windows ENV path.

You can edit env vars in windows by hitting start and typing env should be the first option.

Git bash also installs git and hooks it up with a credentials manager for you and makes it super easy to sign into 2fa-enabled svn services and a ton of other handy developer features.

Git bash is IMO a must on every developer's machine.


Another option: Install WSL (Windows Subsystem for Linux) which will work the same. WSL also lets you install native Linux versions of all your command line applications if you prefer.

Linux binaries will take precedence over windows ones if installed but you can still choose to run the windows version of commands any time you want by specifically adding .exe on the end.