通过 SSH 配置远程 Python 解释器的 Visual Studio 代码

Configuring Visual Studio Code for remote Python interpreter via SSH

我有一个带有 ArchLinux 和 Python 的 Vagrant box,它为每个项目使用一个虚拟环境(通过使用特定的 Python 版本)。我希望为 running/debugging 这些 Python 项目配置 VSC。我已经安装了包含我的项目的目录(使用 sshfs)所以我不必担心同步问题。

使用 PyCharm 配置仅在其 IDE 中。如何使用 SSH 为 VSC 配置它?使用 Python 还需要哪些其他插件?

提前致谢。

PS1: PyCharm 是一个很棒的工具,但它占用大量资源,RAM 接近 1GB。

PS2: 看过this article但不是很清楚,举个例子比较有用

编辑: 我在这里为这个问题写了一个新的改进的答案:vscode python remote interpreter

使用 VScode 终端,您可以 运行 通过 SSH 远程计算机上的 Python 代码:

cat hello_world.py | ssh user@hostname python - 

您可以将其添加为您的 VSCode 构建任务,并 ${file} 指向当前文件。如果在VScode需要远程调试可以阅读以下步骤:code.visualstudio.com/docs/python/debugging#_remote-debugging

此外,您还可以在 .bashrc.zshrc 文件中创建一个 aliasfunction,以便在远程计算机上执行文件,可能在 virtualenv 中, 更方便。例如,我的 .zshrc 文件包含以下函数,用于在远程 virtualenv 中的工作站上执行 Python 文件:

function remote-pytorch () {
    cat  | ssh user@hostname 'source ~/virtualenv/pytorch/bin/activate && python -'
}

这样我就可以运行下面的命令来远程执行脚本了:

remote-pytorch train_network.py

(注意:函数语法在 .bashrc 文件中略有不同)

post Define remote interpreter on remote Linux machine using Pydev and RSE Server 真的很有用,现在看来太明显了。这是我使用自己的系统配置的解决方法:

第 1 步:装载您的远程主文件夹。

$ sshfs -o password_stdin,transform_symlinks vagrant@localhost:/home/vagrant ~/Vagrant/archi02/Remote/ -p 2222 <<< "your_vagrant_password"

第 2 步:使用 VSC 打开项目文件夹。

~/Vagrant/archi02/Remote/Projects/Python_3_7_2/QuickPythonBook/

步骤 3:配置“settings.json”(来自 WorkSpace 设置 ) 用于您的遥控器 Python 和 linter。

{
    "python.pythonPath": "~/Vagrant/archi02/Remote/Projects/Python_3_7_2/QuickPythonBook/ve_qpb/bin/python3.7",
    "python.linting.pylintEnabled": true,
    "python.linting.pylintPath": "pylint"
}

第 4 步:享受编程。不客气。