python - vscode python远程解释器

标签 python visual-studio-code remote-access

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

最佳答案

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

{
    "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": []
        }
    ]
}

请注意,您还可以为执行任务分配一个键绑定(bind),这样您就可以通过一次按键在 Remote 上执行 Python 代码。添加到 keybindings.json:

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

关于python - vscode python远程解释器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53320958/

相关文章:

python - 选择具有所需 data_type 的 pandas 系列并就地应用函数的方法

python - 如何使用 Celery 从 Python 代码动态添加/停止 worker

python - 需要 Microsoft Visual C++ 14.0。用 "Microsoft Visual C++ Build Tools": http://landinghub. visualstudio.com/visual-cpp-build-tools 获取

python - 使用 BeautifulSoup 抓取 Google 时出现语法错误

python-3.x - 在 Visual Studio Code for Mac OS 中使用 Homebrew python3 和 homebrew 包和 pip/pip3 包

visual-studio-code - Visual Studio Code禁用自动报价

windows - Windows 上的 JSCH 使用 mksnt 工具包无法识别目录路径

ipad - 如何在没有服务器或端口转发的情况下远程访问网络

debugging - Visual Studio Code - Golang 调试 : How to see the value of a pointer time. 时间?

visual-studio-code - Visual Studio Code Remote - 通过 KeePass/KeeAgent 使用 key SSH 失败