"No such file or directory" 将 Windows Linux 子系统 bash 与 VS Code 一起使用时

"No such file or directory" when using Windows Linux Subsystem bash with VS Code

我在 Windows 10 上使用 VS Code 和 Windows Linux 子系统 & Ubuntu 18.04.

我想做的是使用 VS Code 作为 python 开发环境,bash 作为终端,python3 解释器安装在 Ubuntu系统作为其默认 python 可执行文件。

在我的用户配置中我有:

"terminal.integrated.shell.windows": "C:\Windows\System32\bash.exe"

设置,在Ubuntu下安装了python3,python是它的别名。

当我尝试执行 python 文件时,出现以下错误:

/usr/bin/python3: can't open file 'c:/Users/R ... /test.py': [Errno 2] No such file or directory

我的感觉是我需要让 VS Code 将相对于 Linux 子系统而不是 Windows C:\ 的路径传递给解释器。我该怎么做?

Linux区分大小写,Windows不区分大小写。您的脚本中有 "c:" 和 "C:"。 也许检查您可能有的任何其他差异?

Python 扩展尚未正式支持 WSL。请参阅 this issue 以跟踪添加支持的状态。

虽然 Visual Studio 代码中似乎没有 Windows 的官方支持,但具有 运行InTerminal 设置的插件 "Code Runner" 解决了这个问题。

它在打开的编辑器的右键单击 window 添加了一个 "Run Code" (Alt-Ctrl-N)。

如果您设置用户设置:

"code-runner.runInTerminal": 真

然后是 运行 代码,它将正确的文件名传递给终端环境的默认可执行文件。

尝试安装扩展程序 Remote Development extension pack,也许可以解决您的问题。 因为它会模拟 WSl 中的 vscode 是 运行。

https://code.visualstudio.com/docs/remote/wsl#_installation