94

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?

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.

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.

dsimcha
  • 67,514
  • 53
  • 213
  • 334
  • 11
    I don't think windows has any sort of shebang support. You could try creating a file association with the python interpreter for .py files. – Nick ODell Sep 27 '11 at 19:16
  • Following up Nic ODell's comment: Anytime I've installed Python on a Windows box the installation process took care of creating the file association for .py files. This has always been using the Python installer for Windows from the python.org site. – GreenMatt Sep 27 '11 at 19:20
  • 3
    See the now current documentation about [Shebang Lines](https://docs.python.org/3/using/windows.html?#shebang-lines) on Windows. Note that you need to run the script using the `py` command for it to have any effect. – martineau Oct 12 '16 at 02:50
  • What if you are using multiple virtual envs? Is there then no way to associate a specific file with a given intepreter? – Sam Redway Aug 07 '20 at 10:54

6 Answers6

61

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.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
  • 1
    e.g. #!"C:\Program Files\Python37\python.exe" – Sébastien Wieckowski Oct 28 '18 at 17:49
  • @tryptofan, yes, quoting is required in general to avoid a security hole, such as the case if "C:\program.exe" exists. "C:\Python33\python.exe" doesn't require quotes, but to be clear I'll update my example to quote the path. – Eryk Sun Oct 29 '18 at 03:40
  • To clarify, you can, in fact, use "linux-only" shebangs (such as `#!/usr/bin/env python3`) on Windows, and it will work correctly on Windows. This is what "virtual shebang" means. See https://docs.python.org/3/using/windows.html#shebang-lines – cowlinator Oct 16 '19 at 22:04
  • @cowlinator, virtual shebangs encompass more than just Unix-style paths. `#!python3` is supported, and we can even extend the launcher with custom commands in "py.ini". – Eryk Sun Oct 17 '19 at 01:04
46

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.

David Z
  • 128,184
  • 27
  • 255
  • 279
Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
  • 8
    It's nice to include the python version number. "python" does not link to "python2.7" in every distro. – mid_kid Dec 11 '13 at 21:22
  • @mid_kid that's why I used env to get the version of python the user has specified as their default. – Spencer Rathbun Dec 12 '13 at 13:11
  • 3
    My point is that that one isn't always the right one. Especially when distributing your script. – mid_kid Dec 12 '13 at 16:28
  • @mid_kid it's the same argument of `/bin/bash` over `/usr/bin/env bash` that people have had. In general, I don't prefer to override the user settings. Especially if they don't have `python2.6` installed, because the default is 2.5, and they installed 2.7 in their home. – Spencer Rathbun Dec 12 '13 at 16:35
  • 8
    The problem starts when your script is incompatible with said python version. Some distro's (For example: ArchLinux) link python with python3. It's generally a good idea to at least specify the major version number: ´#!/usr/bin/env python2´. And maybe your script only works with 2.7, because you use something that only exists in said version. – mid_kid Dec 12 '13 at 20:37
  • 5
    Downvoted because this is answer is outdated: Windows now recognizes shebang lines for Python files. – Jonathan Hartley Apr 26 '18 at 02:00
4

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.

Brad
  • 741
  • 7
  • 17
1

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

John Doe
  • 31
  • 1
  • 9
1

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

Wai Yip Tung
  • 18,106
  • 10
  • 43
  • 47
0

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...

Community
  • 1
  • 1
Chris J
  • 30,688
  • 6
  • 69
  • 111