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 relevant 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.
(*) 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.
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 wonderful features is that it associates *.py files with 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#!/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?
More info: Unlike Cygwin, git bash uses your native windows applications and lets you use bash scripts without any configuration.
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.
There’s also a right-click menu to open bash in any location, as well as a graphical user interface for only 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.
Installing WSL(1 not 2), which is considerably better but requires additional setting, is another alternative. (You can ask me about altering default drive mount routes, manually connecting credential manager, and other things if you wish; there are a lot of modifications I advocate 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