using #!/usr/bin/env python3 shebang with Windows

11,851

Solution 1

No, Windows does not support shebang lines.

The documentation you've linked relates to the py launcher installed by Python, which can interpret various shebang lines to choose a Python version to run a script with.

setuptools is able to generate wrapper .exes for your Python scripts, but it gets a little involved and already assumes you have a package with a setup.py and so on.

Locally, if you really, really need this, you probably could add .py to the PATHEXT environment variable, so the Windows command line looks up .pys like it looks up .exes (and various others; the current modern default is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC). However, this will naturally not scale for distributing apps, as all of your users would need to set that too.

My recommendation is to stick with just that boring old python testing.py, really.

Solution 2

The accepted answer by @AKX is incorrect for Windows 10 standard Python 3, certainly in the latest Windows 10 (1903) if not earlier.

(Note: I cannot speak to how this may or may not work under WSL.)

I have several versions of Python installed (2.7, 3.6, 3.7, and most recently Python 3.8b1). I've been using the #!/usr/bin/env shebang for years in my scripts for cross-platform compatibility (usually to distinguish Py2 vs Py3 scripts).

I've created a little script in a folder (C:\so_test\awtest.py):

#!/usr/bin/env python3.6
import sys
print(sys.version)

If I run this with awtest.py or just awtest I get 3.6.x reported (showing it's running with Python 3.6). If I change the shebang to refer to 3.7, I get 3.7.x reported. If I change the shebang to just #!/usr/bin/env python3 I get the latest version of Python installed (3.8).

Now, if I add that folder to my path (path=%PATH%;C:\so_test in the command window you're testing in, or in the main env vars (you will need to restart the command window if you do the latter though)), I can change to a different directory and run awtest or awtest.py and they still work and refer to the folder in the path. If I remove the script folder from the path these files are no longer found.

While I wouldn't necessarily expect this to work on Windows prior to 10 or Python 2.7, this functionality appears to be the way of things going forward.

Solution 3

You can use shebang in windows by setting the path of your interpreter as the first line in the file(you will see a marker on VSCode that says 'set as interpreter ' on that line). Using windows 10,Python version 3.9 see example:

#!C:/Users/waithira/AppData/Local/Programs/Python/Python39/python.exe 
print('hello world')
Share:
11,851

Related videos on Youtube

MasayoMusic
Author by

MasayoMusic

Currently trying to improve Pandas skills so I can work on personal projects a little faster. Some active users you can learn from: Pandas: jezrael |Scott Boston| YOBEN_S

Updated on June 04, 2022

Comments

  • MasayoMusic
    MasayoMusic almost 2 years

    I'm trying to run a Python script from the command line as a command on Windows -- so no usage of "Python" or ".py". If my script is named "testing.py", I am attempting to make this name into a command and call "testing" from the command line.

    Going through the docs it seems I need to use this shebang #!/usr/bin/env python as long as I have Python in my PATH.

    https://docs.python.org/3/using/windows.html#shebang-lines

    I also have the script folder in my PATH, so something like "testing.py" is currently working from the command line.

    According to the docs and this tutorial, https://dbader.org/blog/how-to-make-command-line-commands-with-python

    I should be able to evoke my Python script just by "testing" if I have the proper paths within PATH and the above shebang. However, I can't seem to get the script running withouth adding ".py".

  • AKX
    AKX over 4 years
    Is this under WSL or with a plain Windows Python?
  • MartyMacGyver
    MartyMacGyver over 4 years
    Regular Windows Python (you cannot use WSL without disabling all other normal virtualization, so I have no way to test it. It's too bad - WSL certainly seems interesting.)
  • AKX
    AKX over 4 years
    Ah, right. I took a look – this is because .py has been associated with the Py launcher which can interpret shebang lines in various creative ways: docs.python.org/3/using/windows.html#shebang-lines – In addition, for just awtest to work, .py must be in PATHEXT on your installation.