Shebang Notation: Python Scripts on Windows and Linux?

PythonWindowsLinuxShebang

Python Problem Overview


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.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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

Solution 4 - Python

Short answer: The easiest way is to install git for windows (git bash) and use shebang lines. #!/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?

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

You can edit env vars in windows by hitting start and typing env should 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.

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.

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

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.


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

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

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

To setup WSL run this command as admin in 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)

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.

Solution 5 - Python

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

Solution 6 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondsimchaView Question on Stackoverflow
Solution 1 - PythonEryk SunView Answer on Stackoverflow
Solution 2 - PythonSpencer RathbunView Answer on Stackoverflow
Solution 3 - PythonWai Yip TungView Answer on Stackoverflow
Solution 4 - PythonBradView Answer on Stackoverflow
Solution 5 - PythonChris JView Answer on Stackoverflow
Solution 6 - PythonJohn DoeView Answer on Stackoverflow