Shebang Notation: Python Scripts on Windows and Linux?

91,132

Solution 1

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

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

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

Solution 4

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.

Share:
91,132

Related videos on Youtube

dsimcha
Author by

dsimcha

Updated on May 20, 2020

Comments

  • dsimcha
    dsimcha almost 4 years

    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.

    • Nick ODell
      Nick ODell over 12 years
      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.
    • GreenMatt
      GreenMatt over 12 years
      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.
    • martineau
      martineau over 7 years
      See the now current documentation about Shebang Lines on Windows. Note that you need to run the script using the py command for it to have any effect.
    • Sam Redway
      Sam Redway almost 4 years
      What if you are using multiple virtual envs? Is there then no way to associate a specific file with a given intepreter?
  • mid_kid
    mid_kid over 10 years
    It's nice to include the python version number. "python" does not link to "python2.7" in every distro.
  • Spencer Rathbun
    Spencer Rathbun over 10 years
    @mid_kid that's why I used env to get the version of python the user has specified as their default.
  • mid_kid
    mid_kid over 10 years
    My point is that that one isn't always the right one. Especially when distributing your script.
  • Spencer Rathbun
    Spencer Rathbun over 10 years
    @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.
  • mid_kid
    mid_kid over 10 years
    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.
  • Jonathan Hartley
    Jonathan Hartley about 6 years
    Downvoted because this is answer is outdated: Windows now recognizes shebang lines for Python files.
  • Sébastien Wieckowski
    Sébastien Wieckowski over 5 years
    e.g. #!"C:\Program Files\Python37\python.exe"
  • Eryk Sun
    Eryk Sun over 5 years
    @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.
  • cowlinator
    cowlinator over 4 years
    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 docs.python.org/3/using/windows.html#shebang-lines
  • Eryk Sun
    Eryk Sun over 4 years
    @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".