I have resolved this issue. If anyone wants to do remote development and debugging, follow below steps:
- Install remote ssh extension in VS code
- Once installed, you will find a green icon on the bottom left corner in vs code which allows us to connect to the remote machine.
- Connect to the remote machine using the standard ssh command. Alternatively, you can use ssh-keygen to generate a public-private key if you don't want to use the password at every prompt.
- Once you are connected to remote machine, you can open the file explorer and create any python file. When you will save this, it will get saved in your remote machine. This way you are using your machine to remotely develop code on another remote machine.
- Good thing about vs code is that it selects the remote machine's python interpreter so all the packages which you have installed on your remote machine will work with IntelliSense.
- In order to debug the code, we will use debugpy. Install this on both machine (remote & local)
- On your remote machine, run below command:
python3 -m debugpy --listen 1.2.3.4:5678 --wait-for-client app.py
here 1.2.3.4 is the IP of remote machine. This will start a remote debugger which will wait for a clients connection.
- On your local machine, in VS code open Run & Debug, add a configuration of
Python: Remote Attach. Make sure thatlaunch.jsonhas the host as the IP of your remote machine and port as 5678. - Now start debugging as normal and you will notice the code will break at first breakpoint and from here you can proceed normally as we used to do in local debugging process.
TBH, this is best feature VS code has because most of the software allows you to do remote development which is nothing but just a normal SSH but remote debugging gives you more control. I was doing some python project on Raspberry Pi and obviously cannot install VS code or pycharm on it. But with this feature now I can easily develop the code using Pi's python interpreter and debug it as well.
If anyone is having any issues, let me know. Happy to help.
Answer from S Andrew on Stack OverflowI have resolved this issue. If anyone wants to do remote development and debugging, follow below steps:
- Install remote ssh extension in VS code
- Once installed, you will find a green icon on the bottom left corner in vs code which allows us to connect to the remote machine.
- Connect to the remote machine using the standard ssh command. Alternatively, you can use ssh-keygen to generate a public-private key if you don't want to use the password at every prompt.
- Once you are connected to remote machine, you can open the file explorer and create any python file. When you will save this, it will get saved in your remote machine. This way you are using your machine to remotely develop code on another remote machine.
- Good thing about vs code is that it selects the remote machine's python interpreter so all the packages which you have installed on your remote machine will work with IntelliSense.
- In order to debug the code, we will use debugpy. Install this on both machine (remote & local)
- On your remote machine, run below command:
python3 -m debugpy --listen 1.2.3.4:5678 --wait-for-client app.py
here 1.2.3.4 is the IP of remote machine. This will start a remote debugger which will wait for a clients connection.
- On your local machine, in VS code open Run & Debug, add a configuration of
Python: Remote Attach. Make sure thatlaunch.jsonhas the host as the IP of your remote machine and port as 5678. - Now start debugging as normal and you will notice the code will break at first breakpoint and from here you can proceed normally as we used to do in local debugging process.
TBH, this is best feature VS code has because most of the software allows you to do remote development which is nothing but just a normal SSH but remote debugging gives you more control. I was doing some python project on Raspberry Pi and obviously cannot install VS code or pycharm on it. But with this feature now I can easily develop the code using Pi's python interpreter and debug it as well.
If anyone is having any issues, let me know. Happy to help.
It is frustrating there are so many out-dated instructions how to remote debug Python scripts from VSC.
As of August 2024 there is a far easier way to set this up that answers the OP's question which involved VSC being installed on a laptop and debugging a Python script saved on a Raspberry Pi also in the same LAN.
Once you have installed the Remote SSH extension on your local machine use that to SSH via VSC in to your remote machine. Now install the Python Debugger extension on your remote system.
Since you have ssh'd in to that remote system via the VSC extension VSC just debugs the script on that remote system as though it is LOCAL.
- Place a breakpoint (red dot) on the line you want script execution to stop at.
- From File Menu > Run > Start Debugging (F5)... The Command Palette will open and it will say:
- Select Debugger <-- Choose Python Debugger
- Then choose...
- Python File Debug the currently active Python File.
- Now use F10, F11 etc to step through the lines of code.
You DO NOT need to...
- Do ssh local port forwarding.
- Separately install debugpy from the command line in your local and remote environments.
- Run debugpy from the command line.
- Have a copy of the Python script in your local environment AND another on the remote environment. You only need ONE copy in the remote environment.
- You don't even need a launch.json.
I checked about what sockets were opened by separately ssh'ing from a Terminal in to the "remote system" (my Raspberry Pi running Ubuntu) and they are all localhost.
ubuntu@ubuntu:~$ lsof -i
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
code-f1e1 76603 ubuntu 9u IPv4 519530 0t0 TCP localhost:43675 (LISTEN)
code-f1e1 76603 ubuntu 12u IPv4 519534 0t0 TCP localhost:43675->localhost:34540 (ESTABLISHED)
python 77190 ubuntu 4u IPv4 520559 0t0 TCP localhost:39819 (LISTEN)
python 77190 ubuntu 8u IPv4 522535 0t0 TCP localhost:45381 (LISTEN)
python 77190 ubuntu 9u IPv4 521557 0t0 TCP localhost:45381->localhost:59082 (ESTABLISHED)
python 77190 ubuntu 11u IPv4 521558 0t0 TCP localhost:35979->localhost:37454 (ESTABLISHED)
python 77218 ubuntu 3u IPv4 520157 0t0 TCP localhost:37454->localhost:35979 (ESTABLISHED)
python 77228 ubuntu 3u IPv4 520164 0t0 TCP localhost:59082->localhost:45381 (ESTABLISHED)
How to "develop locally, run/debug remotely" for Python?
visual studio code - Setting up Python remote debugging in VSCode - Stack Overflow
Remote debugging configuration suddenly not working anymore
Debugging dockerized Python apps in VSCode
Videos
Hi r/vscode!
I'm relatively new to VS Code (and developing in general), so please forgive any egregious lapses in my knowledge.
How remote development worked in PyCharm
I come from PyCharm, where I could write Python scripts locally and run/debug them on a remote machine I had SSH access to. PyCharm would automatically sync my files to the remote server, so the latest version of my code was always on my local machine while any output generated would be on my remote machine.
I've tried to set this up in VS Code...
I've installed VS Code, the Python extension, the Remote-SSH extension, and the Sync-RSync extensions. When I write code locally, I've set Sync-Rsync to automatically sync my changes to the remote server every time I save, which is great! However, to run/debug my code remotely, I need to:
open a new VS Code window SSHed to my remote server
navigate to and open my workspace there
find the synced copy of the script I just edited
hit run/debug
Now I have two windows to manage, with their own files and workspaces! I often forget which machine I'm editing on and have to backtrack and copy changes manually across machines.
I'm sure I'm missing something trivial in my setup, but I'm not sure what. Could you please help? How do I write code in exactly one VS Code window (running locally) and run/debug it in a remote environment?