Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run a local python script with a remote ssh interpreter via Visual Studio Code?

Is it possible to run a local python script with a remote ssh interpreter via Visual Studio Code?

I have a python project on a remote Linux server machine, and I want to copy the code to my local machine and debug it via remote python interpreter with all its accesses and permissions to databases etc.

like image 607
Vladimir Strekalovskiy Avatar asked Apr 07 '21 07:04

Vladimir Strekalovskiy


People also ask

How do I run a local Python script on a remote server?

Using the paramiko library - a pure python implementation of SSH2 - your python script can connect to a remote host via SSH, copy itself (!) to that host and then execute that copy on the remote host. Stdin, stdout and stderr of the remote process will be available on your local running script.

Can you run Python on Visual Studio Code?

Visual Studio Code is a free source code editor that fully supports Python and useful features such as real-time collaboration.

Can you SSH with Visual Studio?

You can connect over SSH into another machine from Visual Studio Code and interact with files and folders anywhere on that remote filesystem. If you have an app located on a different computer, you could use SSH to connect to it and access your app, view its files, and even modify, run, and debug it.

How to run a python script remotely via SSH on a Windows?

How to run a python script remotely via SSH on a Windows machine 1 I need to transfer a zipped folder to a Windows server, 2 unzip the folder and 3 then run a certain python file within the folder. More ...

How do I use SSH in Visual Studio Code?

Remote Development using SSH The Visual Studio Code Remote - SSH extension allows you to open a remote folder on any remote machine, virtual machine, or container with a running SSH server and take full advantage of VS Code's feature set. Once connected to a server, you can interact with files and folders anywhere on the remote filesystem.

How do I SSH into VS Code using remote-SSH?

With the Remote - SSH extension installed, you will see a new Status bar item at the far left. The Remote Status bar item can quickly show you in which context VS Code is running (local or remote) and clicking on the item will bring up the Remote - SSH commands.

Can you run Python on a remote machine?

Another common setup Python developers have is that their development environment is running on a remote machine, often because the remote machine has access to resources or data sets that are not available on the local machine. Like the docker scenario, you can use the “Remote-SSH” extension to open a remote workspace over an SSH connection.


2 Answers

If you have the ability to install rmate on the remote server, then you can use Remote VSCode.

For this, extension's documentation describes, you configure the VS Code user settings for the extension, start the extension's server from the command palette with Remote: Start server, open an ssh connection to the remote server with a remote port forwarded with ssh -R 52698:localhost:52698 [email protected], and then you run rmate -p 52698 <file> on the remote server.

This will cause the specified file to open up in your local VS Code editor, and any changes made to it there will be reflected in the copy on the remote server very soon after you save them.


Alternatively, you can use sshfs to mount the remote directory as if it were a local directory. This is my preferred method nowadays for editing remote files using a local editor. On systems using apt you can install it with apt install sshfs. On systems using yum you can install it with yum install fuse-sshfs.

Once installed, make a local directory in which you'll mount the remote directory. We'll call it /mnt/remote for this example. Go ahead and mount it with sshfs:

sshfs -o allow_other,default_permissions [email protected]:<src dir> /mnt/remote

Where <src dir> is the absolute path to the remote directory you would like local access to.

Add the IdentityFile=/home/$USER/.ssh/id_rsa option after default_permissions if you are using key authorization, as you ought to be.

You may need to tweak the permissions of /mnt/remote, and use sudo for all of the commands listed above.

Many other options are detailed in the man pages. Consider also using large_read for greater efficiency, and reconnect,ServerAliveInterval=5,ServerAliveCountMax=15 for greater reliability.

Once the sshfs command succeeds, you should find that the remote directory is accessible as the local directory /mnt/remote. To edit the contents of the remote locally, simply open up VS Code to that directory with code /mnt/remote, and edit away as if it were all local! Your changes will be reflected in the remote files very soon after you save them.

like image 150
Will Da Silva Avatar answered Oct 29 '22 07:10

Will Da Silva


You can have a fully remote development environment using Remote SSH extension to access you remote server. Currently this is the most convenient option. I guess you already know about this extension. I've being using it for about a year, it works fine for me. The "restart" issue is most probably connected to combination of "Amazon Linux" (based on CentOS) kernel and out-of-memory problem. Make sure you have >4Gb of memory available (on a remote machine) as VSCode "eats up" at least 1Gb. And once you are out of memory the remote machine will most probably just hang. Try to change to Ubuntu based kernel, if you still have troubles.

Another option is to run Jupyter Lab/Notebook on remote machine. This will allow you to edit, run and debug code in the local browser window. (You will have to keep some port open on remote and connect using ssh -L option).

Another possible solution is to use extensions like Sync-Rsync or Remote VSCode to sync/edit remote code in your local VSCode instance or you can use any GUI-based SSH client to edit the remote code locally. But this way you're loosing the ability to run/debug remotely.

I will also mention that PyCharm (professional edition is required) have the ability to edit files locally, then sync and run/debug on remote.

like image 24
igrinis Avatar answered Oct 29 '22 09:10

igrinis