如何在Windows中使用Shebang进行Python?

发布于 2025-01-21 09:25:49 字数 1041 浏览 1 评论 0原文

代码

#! 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.

Edit:
Without space
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄想挽回 2025-01-28 09:25:50

Shebangs不是Windows的东西。

在Windows上,它必须依靠第三方软件:Bash,Cygwin,Python Launcher等。因此,您将在这些第三方软件文档中找到有关其支持的大多数信息。

但是第一件事,我将尝试删除#!和解释器的完整路径之间的空间。 Shebang和口译员通常不会被空间隔开。

#!C:\Python310\python.exe

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
顾北清歌寒 2025-01-28 09:25:50

py.exe位置中添加py.ini文件。

[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

注意最后一个字符(使用任何);没有此,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.

[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文