Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring Visual Studio Code for remote Python interpreter via SSH

I have a Vagrant box with ArchLinux and Python which uses a virtual environment per project (by using a certain Python version). I wish to configure VSC for running/debugging these Python projects. I've mounted the directory containing my projects (with sshfs) so I don't have to worry about sync.

With PyCharm the configuration is only in its IDE. How can I configure it for VSC by using SSH? What are other plugins necessary to work with Python?

Thanks in advance.

PS1: PyCharm is a great tool but it takes much resources, near 1GB in RAM.

PS2: I've read this article but it is not clear for me, one example is more useful.

like image 349
Ουιλιαμ Αρκευα Avatar asked Oct 25 '18 15:10

Ουιλιαμ Αρκευα


People also ask

How do I add Python interpreter to Visual Studio Code?

To do so, open the Command Palette (Ctrl+Shift+P) and enter Preferences: Open User Settings. Then set python.defaultInterpreterPath , which is in the Python extension section of User Settings, with the appropriate interpreter.

How does Visual Studio Code connect to SSH server?

In VS Code, select Remote-SSH: Connect to Host... from the Command Palette (F1, Ctrl+Shift+P) and use the same user@hostname as in step 1. If VS Code cannot automatically detect the type of server you are connecting to, you will be asked to select the type manually.

How add SSH key to VS Code?

Add SSH key to your VM# Select Use existing public key in the dropdown for SSH public key source so that you can use the public key you just generated. Take the public key and paste it into your VM setup, by copying the entire contents of the id_ed25519. pub in the SSH public key.

How do I run a Python program in Visual Studio Code terminal?

Just click the Run Python File in Terminal play button in the top-right side of the editor. Select one or more lines, then press Shift+Enter or right-click and select Run Selection/Line in Python Terminal. This command is convenient for testing just a part of a file.


Video Answer


2 Answers

The post Define remote interpreter on remote Linux machine using Pydev and RSE Server was really useful, it seems so obvious now. This is my workaround using my own system configuration:

Step 1: Mount your remote home folder.

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

Step 2: Open your project folder with VSC.

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

Step 3: Configure "settings.json" (from WorkSpace Settings) for your remote Python and 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"
}

Step 4: Enjoy programming. You are welcome.

like image 180
Ουιλιαμ Αρκευα Avatar answered Nov 14 '22 22:11

Ουιλιαμ Αρκευα


EDIT: I wrote a new and improved answer to this question here: vscode python remote interpreter

Using the VScode terminal you can run the Python code on a remote machine over SSH with:

cat hello_world.py | ssh user@hostname python - 

You can add this as as your VSCode build task with ${file} pointing to the current file. If you need remote debugging in VScode you can read the following steps: code.visualstudio.com/docs/python/debugging#_remote-debugging

Additionally, you could also create an alias or function in your .bashrc or .zshrc file that makes executing files on a remote machine, potentially in a virtualenv, more convenient. For example, my .zshrc file contains the following function to execute Python files on my workstation in a remote virtualenv:

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

This way, I can just run the following command to execute the script remotely:

remote-pytorch train_network.py

(note: the syntax for functions is slightly different in .bashrc files)

like image 23
verified.human Avatar answered Nov 14 '22 22:11

verified.human