skip to Main Content

I am doing Python development in Raspberry Pi. I have installed VS Code in my laptop and have installed the ssh extension. Using this I can easily connect to Raspberry Pi. While I am connected I can see that VS Code has also loaded the Python interpreter of Raspberry Pi. I can run my Python script from within the vs code but when I tried to debug the code, nothing happens.

Is it possible to remotely debug the Python script from laptop to Raspberry Pi? How can I enable this?

2

Answers


  1. Chosen as BEST ANSWER

    I have resolved this issue. If anyone wants to do remote development and debugging, follow below steps:

    1. Install remote ssh extension in VS code
    2. 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.
    3. 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.
    4. 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.
    5. 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.
    6. In order to debug the code, we will use debugpy. Install this on both machine (remote & local)
    7. 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.

    1. On your local machine, in VS code open Run & Debug, add a configuration of Python: Remote Attach. Make sure that launch.json has the host as the IP of your remote machine and port as 5678.
    2. 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.


  2. To highly simplify the remote debugging process between two windows machines, make is universal for all IDEs and avoid SSH file permission errors on Windows, I made my own python library that solves this problem.

    Just run pip install pywinrd and create those two scripts:-

    Server.py

    from PyWinRD.Server import WinRDServer
    
    server = WinRDServer()
    server.start()
    

    Client.py

    from PyWinRD.Client import WinRDClient
    
    client = WinRDClient()
    client.connect()
    
    client.deploy('path/to/file/or/folder')
    client.debug('path/to/python/script')
    client.terminal('termninal command')
    
    client.disconnect()
    

    Run Server.py on the host Windows machine, and Client.py on your machine, you will have the ability to execute terminal commands, deploy extra files/folders and debug any python script you want on the host machine.

    A copy of all the stdout, stderr calls on the python script you are debugging will be redirected to your machine in real-time (ex. print and raise statements).

    All the stdin calls will be redirected to your machine only (ex. input statements), so the server will not interfere with them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search