Home > Net >  How to use shebang in windows for Python?
How to use shebang in windows for Python?

Time:04-15

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

CodePudding user response:

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

CodePudding user response:

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. enter image description here

  • Related