如何在 Windows 10 中配置 Visual Studio Code 以忽略 shebangs 并使用 Python 解释器路径?

WoJ*_*WoJ 5 python python-3.x visual-studio-code

我有一个旨在在 Linux 中运行的 Python 项目:

#!/usr/bin/env python3
def hello(a: str):
    print(f"bonjour {a}")

hello("SO")
Run Code Online (Sandbox Code Playgroud)

当我使用 Visual Studio Code 在 Windows 上编辑此代码并运行它时,我得到

[Running] /usr/bin/env python3 "d:\Seafile\dev\dev-perso\domotiqueNG\services\dispatcher\hello.py"
The system cannot find the path specified.
Run Code Online (Sandbox Code Playgroud)

如果 Visual Studio Code 打算实际使用 shebang,则错误是可以理解的:既不env也不python3存在。

我应该如何配置 Visual Studio Code,以便它不考虑 shebang 而使用C:\Python36\python.exe可执行文件?


我在设置中找到Python: Python Path并将其设置为我的可执行文件,但由于存在 shebang,shebang 优先

我删除了 shebang 来尝试该版本(这不是解决方案,我需要将 shebang 保留在那里),有趣的是我得到了:

[Running] python -u "d:\Seafile\dev\dev-perso\domotiqueNG\services\dispatcher\hello.py"
  File "d:\Seafile\dev\dev-perso\domotiqueNG\services\dispatcher\hello.py", line 2
    def hello(a: str):
               ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为它似乎表明编译器无法识别 Python 3.6 语法,而 Python 3.6 是路径中的语法(计算机上隐藏着另外两个 Python 2 可执行文件,甚至不在路径中)。

Visual Studio Code 建议使用 3.6:

在此输入图像描述

所以我怀疑在某个地方可以调整Python可执行路径的设置。

WoJ*_*WoJ 4

舍邦

可以在设置中配置是否使用 shebang:搜索shebang,然后Code-runner: Respect Shebang

Python 的版本

当通过正确的解释器运行代码时CtrlF5使用。

AltCtrlN当通过( )运行时Run Code,使用了错误的。

我检查过

import sys
print(sys.executable)
Run Code Online (Sandbox Code Playgroud)

由于某种原因,显示了 Platform.io 解释器。我不知道它是如何结束的Run Code,但禁用 Platform.io 有帮助。现在两者AltCtrlNCtrlF5使用正确的解释器。

我仍然不知道为什么两种启动脚本的方式使用不同的解释器,但至少现在问题已经解决了。