vscode python 远程解释器 [英] vscode python remote interpreter

查看:138
本文介绍了vscode python 远程解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用 VSCode (Visual Studio Code),我在本地 Python (Anaconda) 解释器上执行 Python 代码.现在我想设置它,以便我能够在远程 Python 解释器上执行该代码.我有一个 Linux 设备,它有自己的 Python 并且可以通过 ssh 访问.
可以配置吗?如果是这样怎么办?谢谢.

By using VSCode (Visual Studio Code) I execute Python code on a local Python (Anaconda) interpreter. Now I would like to set it up so that I am able to execute that code on a remote Python interpreter. I have a Linux device which has its own Python and is accessible via ssh.
Is it possible to configure it? If so how? Thank you.

推荐答案

虽然微软正致力于在 VSCode 中正式实现这一点(请参阅:https://github.com/Microsoft/vscode-python/issues/79) 我个人使用 tasks.json 中定义的以下任务用于在我的远程机器上运行 Python.它包含两个任务:(1)使用 rsync 将代码同步到远程机器;(2) 在远程解释器中通过 SSH 执行代码.请注意,执行任务 dependsOn 是同步任务,因此执行代码总是从最新的本地副本开始.

While Microsoft is working on officially implementing this in VSCode (see: https://github.com/Microsoft/vscode-python/issues/79) I am personally using the following task defined in tasks.json for running Python on my remote machine. It contains two tasks: (1) synchronize the code to the remote machine using rsync; (2) execute the code over SSH in the remote interpreter. Note that the execution task dependsOn the sync task so that executing the code is always done from the latest local copy.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Synchronize Code",
            "type": "shell",
            "command": "rsync -axv --exclude-from=rsync-exclude.lst --max-size=5MB \"${workspaceFolder}\" user@hostname:dev/code-sync/",
            "problemMatcher": [],
            "isBackground": true,
            "presentation": {
                "echo": false,
                "reveal": "silent",
                "focus": false,
                "panel": "shared",
                "clear": false
            }
        },
        {
            "label": "Remote Execute",
            "type": "shell",
            "command": "ssh -n user@hostname \"source ~/.profile && source /path/to/virtualenv/bin/activate && python ~/dev/code-sync/${workspaceFolderBasename}/${relativeFile}\"",
            "dependsOn": [
                "Synchronize Code"
            ],
            "problemMatcher": []
        }
    ]
}

请注意,您还可以为执行任务分配键绑定,以便您可以通过一次按键在遥控器上执行 Python 代码.添加到keybindings.json:

Note that you can also assign a keybinding to executing the task so that you can execute the Python code on the remote with a single keypress. Add to keybindings.json:

{
    "key": "cmd+shift+r",
    "command": "workbench.action.tasks.runTask",
    "args": "Remote Execute"
}

这篇关于vscode python 远程解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆