13

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.

1

1 Answer 1

6

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

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"
}
Sign up to request clarification or add additional context in comments.

2 Comments

But this doesn't help with features like auto-complete, right? Because that's also an important implication of builtin remote interpreter. How do you go about solving that? Do you create the same exact environment at local too?
Also, why are you not using the sync-rsync plugin?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.