Coder Perfect

Python Scripts on Windows and Linux Using Shebang Notation?

Problem

I’ve built some small Python utility scripts that I’d want to run on both Windows and Linux. I don’t want to have to call the Python interpreter explicitly. On both Windows and Linux, is there a simple way to point shebang notation to the correct locations? If not, is there another method to allow implicit Python interpreter invocation on both Windows and Linux without having to change the script when switching operating systems?

Edit: Cygwin has shebang support on Windows, however I prefer to use the native Windows Python interpreter rather than the Cygwin one.

In Cygwin terminals, it appears that shebang notation overrides file associations. I suppose I could uninstall Cygwin Python and create a symlink from /usr/bin/python to Windows-native Python.

Asked by dsimcha

Solution #1

In the documentation, look up the Python Launcher for Windows, which was first defined in PEP 397. It allows you to declare custom shebang configurations in “py.ini” (for example, to use pypy), and it supports virtual shebangs like #!/usr/bin/env python3, as well as shebangs with real paths like #!”C:Python33python.exe” out of the box. (For paths with spaces, quoting is required.) A shebang can also have command-line parameters added to it. For example, the shebang #!/usr/bin/python3 -i adds the option to enter interactive mode when the script finishes.

In order to enable shebang support for scripts in Windows, the installer associates.py (console) and.pyw (GUI) script file types with the respective named launchers, py.exe and pyw.exe. The launchers are installed in the Windows folder for an all-users installation (i.e. percent SystemRoot percent ). In order to run py.exe in the shell with a per-user installation, you may need to explicitly add the installation location to PATH (*). Then you may use py -2, py -3, py -2.6, py -3.3-32 (32-bit), and so on to run Python from the command line. When paired with -m, the launcher can be used to run a module as a script using a certain version of the interpreter, for as py -3 -m pip install.

(*) Instead of installing the launcher alongside “python.exe,” the new installer in 3.5+ defaults to “percent LocalAppData percent ProgramsPythonLauncher” for a per-user installation, and it automatically adds this directory to PATH.

Answered by Eryk Sun

Solution #2

Unless you’re using cygwin, Windows doesn’t support shebangs. When you install Python, however, it creates a file association for.py files. It will run through Python if you just type the name of your script on the command line or double-click it in Windows Explorer.

In my scripts, I include a #!/usr/bin/env python shebang. On Linux, this enables shebang functionality. If you run it on a Windows computer with Python installed, the file association should be present, and it should work.

Answered by Spencer Rathbun

Solution #3

pywin32 is a Python interpreter for Windows. One of the nice thing is it setups the file association of *.py to the python interpreter.

Answered by Wai Yip Tung

Solution #4

Installing git for Windows (git bash) and using shebang lines is the simplest way. #!/usr/bin/env python||anyothercommand

It’s the first thing I install on any computer since who doesn’t want bash, and we’re all familiar with git, right?

Unlike Cygwin, git bash leverages your native Windows apps and allows you to run bash scripts without having to configure anything.

It will treat any file containing a shebang line as executable and run the command if it is in your path, similar to how bash on Linux does. It also only works with Windows ENV variables, so everything you put to your path to work with Powershell or cmd will also work with git bash.

In Windows, you can edit env vars by pressing start and typing env as the first option. Simply modify your user path var (or the global one for all users) and add any command-line applications you wish.

Also installs git and configures it to work with Windows Credential Manager, making it incredibly simple to sign onto 2FA enabled svn services like GitHub and Bitbucket without having to produce tokens.

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

To be nice to your Linux and macOS friends, tell it to checkout as-is and commit as Unix line endings upon installation. Many alternative git settings are also recommended, but that’s an other matter.

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, while more complex, allows you to have separate Linux and Windows versions of all your command line apps.

It can still run native Windows exe files if you desire, but if Linux binaries are installed, they will take precedence.

Run the following command in power shell as an administrator to set up WSL:

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

*requires reboot

Then look for Linux in the Microsoft app store and choose a distro. (Ubuntu 18LTS is my recommendation.)

PS: You can also use git bash to run WSL. Oh, and if you’re using PHP, windows composer.exe will also execute WSL using shebang lines.

Answered by Brad

Solution #5

Not with shebang… but you might be able to set up a file association; see this SO question about Perl and the responses; they’ll also be useful because there are known issues with Windows with stdin/out redirection…

Answered by Chris J

Post is based on https://stackoverflow.com/questions/7574453/shebang-notation-python-scripts-on-windows-and-linux