Setting up Python remote debugging in VSCode

2020-06-06 02:32发布

I am having my remote(ubuntu 16.04) drive mounted on my local system(ubuntu 16.04) so that I can edit the source files by opening them in vscode.

Also, in the integrated terminal I can ssh to remote system and run the program using remote python interpreter which is installed on virtual environment like:

$ssh username@remoteip    
$workon remotevirtualenv
(remotevirtualenv)$python source.py

I want to enable remote debugging so that if I run the debug/run the remote files, the vscode which is installed on my local system uses my remote python interpreter.

I went through the docs suggesting the use of ptvsd extension(which asks to have 2 copies of source files one on local, another one server) but I am not sure how to configure it in this scenario. Appreciate the help. Thanks.

Edit 1: As, I said I have gone through the docs but I am not clear how to configure in this scenario. e.g. docs say

  1. In the source code on both computers, add the following lines, replacing my_secret with the appropriate passphrase to authenticate remote debugging, and replacing address with the appropriate IP address (or localhost) and port number:
ptvsd.enable_attach("my_secret", address = ('0.0.0.0', 3000))

But I have only one copy of source file i.e. on remote system. I have just mounted it on my local file system. So, should I give my local ip address or it should be remote system ip and which port number should I use as I am having only one copy of source and hence ptvsd, how is it going to communicate. Also, in the configuration, what should I use for localRoot location and remoteRoot location.

 3. {
     "name": "Attach (Remote Debug)",
     "type": "python",
     "request": "attach",
     "localRoot": "${workspaceFolder}",
     "remoteRoot": "${workspaceFolder}",
     "port": 3000,
     "secret": "my_secret",
     "host": "localhost" 
 }

Edit: Thanks to remote development extension in VSCode, remote debugging is super easy now.

2条回答
劫难
2楼-- · 2020-06-06 03:17

You need to put the remote ip address in both ptvsd.enable_attach("my_secret", address = ('remote_ip_address', 3000)) and in launch.json:

{
  "name": "Attach (Remote Debug)",
  "type": "python",
  "request": "attach",
  "localRoot": "${workspaceFolder}",
  "remoteRoot": "/home/user1/scripts/",
  "port": 3000,
  "secret": "my_secret",
  "host": "remote_ip_address" 
}

You also need to change the remoteRoot value to the path of the directory where the script is located in the remote machine (e.g. /home/user1/scripts/).

Finally, open an ssh connection: ssh -L 3000:localhost:3000, run your script in the remote machine and attach the debugger in the local machine.

查看更多
Viruses.
3楼-- · 2020-06-06 03:26
登录 后发表回答