首页 > 解决方案 > 使用带有 VS Code 的 Windows Linux 子系统 bash 时出现“没有这样的文件或目录”

问题描述

我在带有 Windows Linux 子系统和 Ubuntu 18.04 的 Windows 10 上使用 VS Code。

我正在尝试做的是使用 VS Code 作为 python 开发环境,以 bash 作为其终端,并将安装在 Ubuntu 系统上的 python3 解释器作为其默认的 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:\ 的路径传递给解释器。我怎样才能做到这一点?

标签: pythonwindowsvisual-studio-codewindows-subsystem-for-linux

解决方案


虽然 Visual Studio Code for Windows 似乎没有官方支持,但带有 runInTerminal 设置的插件“Code Runner”解决了这个问题。

它将“运行代码”(Alt-Ctrl-N)添加到打开的编辑器的右键单击窗口中。

如果您设置用户设置:

“code-runner.runInTerminal”:真

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


推荐阅读