简体   繁体   English

如何使用 windows 中的 shebang 为 Python?

[英]How to use shebang in windows for Python?

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: 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编辑:没有空间在此处输入图像描述

Shebangs aren't a Windows thing. Shebangs 不是 Windows 的东西。

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. Windows 必须依赖第三方软件:bash、cygwin、python launcher 等。因此您可以在这些第三方软件的文档中找到有关其支持的大部分信息。

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. Shebang 和解释器通常不被空格分隔。

#!C:\Python310\python.exe

Add py.ini file in the py.exe location.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

Note the last char (use any);注意最后一个字符(使用任何字符); Without this, pylauncher not works.没有这个,pylauncher 将无法工作。 (2nd outptut). (第二个输出)。 Probably a bug.可能是一个错误。 Repo回购

在此处输入图像描述

Edit:编辑:

Shebangs are not working with py -u Shebangs 不与py -u一起工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM