4

Code

#! C:\Python310\python.exe
import os
import sys
print(f"PY_PYTHON={os.getenv('PY_PYTHON')}")
print(f"{sys.version=}")
print(f"Shebang : '{open(__file__).read().splitlines()[0]}'")
cmd = 'py -0p'
print(cmd)
os.system(cmd)
cmd = f'file {__file__}'
print(cmd)
os.system(cmd)

Output:

λ py t9.py
PY_PYTHON=None
sys.version='3.11.0a7 (main, Apr  5 2022, 21:27:39) [MSC v.1929 64 bit (AMD64)]'
Shebang : '#! C:\Python310\python.exe'
py -0p
 -V:3.11 *        C:\Python311\python.exe
 -V:3.10          C:\Python310\python.exe
 -V:3.9           C:\Users\smart\AppData\Local\Programs\Python\Python39\python.exe
file C:\Users\smart\Desktop\budhubot\budhubot\t9.py
C:\Users\smart\Desktop\budhubot\budhubot\t9.py: a  C:\Python310\python.exe script, ASCII text executable, with CRLF line terminator

Tried forward slash too.

Edit: Without space enter image description here

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59

2 Answers2

2

Shebangs aren't a Windows thing.

On Windows, it must rely on a third-party software: bash, cygwin, python launcher, etc. So you'll find most information about its support in these third-party softwares' documentation.

But first thing, I would try to remove the space between #! and the interpreter's full path. Shebang and interpreter aren't separated by a space usually.

#!C:\Python310\python.exe
Wisblade
  • 1,483
  • 4
  • 13
  • Tried. edited qn. FYI, Space used in [docs](https://docs.python.org/3/using/windows.html#:~:text=%23!%20%2Fusr%2Fbin%2Fpython,starting%20with%20%2Fusr.) – Smart Manoj Apr 14 '22 at 09:11
  • Doc don't show a SINGLE example of shebang + windows path... Only Unix portable virtual commands. Maybe it's the problem? Or maybe search in PyLauncher's support? – Wisblade Apr 14 '22 at 10:12
  • syntax https://peps.python.org/pep-0397/#:~:text=%23!%20interpreter%20%5Boptional-arg,the%20following%20rules%3A – Smart Manoj Apr 14 '22 at 14:02
  • Yeah, but still no examples of shebangs with Windows paths... Look at the other answer, maybe a clue. It seems that it doesn't work "out-of-the-box". – Wisblade Apr 14 '22 at 15:46
  • examples are only derived from the syntax. FYI, I wrote the other answer. – Smart Manoj Apr 15 '22 at 02:37
  • @SmartManoj Oh, right, my bad, for the other answer. What I meant is that documentation, despite being mostly for Windows, lacks examples for Windows and shows only Unix paths - while PyLauncher being near irrelevant on Linux, since bash & other shells support shebangs natively. – Wisblade Apr 15 '22 at 07:15
0

Add py.ini file in the py.exe location.

[commands]
py311="C:\Python311\python.exe"6
py310="C:\Python310\python.exe"6
py39="C:\Users\smart\AppData\Local\Programs\Python\Python39\python.exe"6

Note the last char (use any); Without this, pylauncher not works. (2nd outptut). Probably a bug. Repo

enter image description here

Edit:

Shebangs are not working with py -u

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59