如何在Windows中使用Shebang进行Python?
代码
#! 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)
输出:
λ 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
也尝试过前斜线。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Shebangs不是Windows的东西。
在Windows上,它必须依靠第三方软件:Bash,Cygwin,Python Launcher等。因此,您将在这些第三方软件文档中找到有关其支持的大多数信息。
但是第一件事,我将尝试删除
#!
和解释器的完整路径之间的空间。 Shebang和口译员通常不会被空间隔开。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.在
py.exe
位置中添加py.ini文件。注意最后一个字符(使用任何);没有此,Pylauncher无法正常工作。 (第二支球)。可能是一个错误。 ”
” https://i.sstatic.net/81uyw.png“ alt alt = ”
/81Uyw.png“ rel =” nofollow noreferrer“>
py -u
Add py.ini file in the
py.exe
location.Note the last char (use any); Without this, pylauncher not works. (2nd outptut). Probably a bug. Repo
Edit:
Shebangs are not working with
py -u