在 VSCode 中设置 Python 远程调试 [英] Setting up Python remote debugging in VSCode

查看:83
本文介绍了在 VSCode 中设置 Python 远程调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的远程(ubuntu 16.04)驱动器安装在我的本地系统(ubuntu 16.04)上,以便我可以通过在 vscode 中打开它们来编辑源文件.

此外,在集成终端中,我可以通过 ssh 连接到远程系统并使用安装在虚拟环境中的远程 python 解释器运行程序,例如:

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

我想启用远程调试,这样如果我运行调试/运行远程文件,安装在我本地系统上的 vscode 使用我的远程 python 解释器.

我浏览了建议使用 ptvsd 扩展的文档(要求在本地有 2 个源文件副本,一个在本地,另一个在服务器上),但我不确定如何在这种情况下配置它.感谢帮助.谢谢.

编辑 1:因为,我说我已经阅读了文档,但我不清楚如何在这种情况下进行配置.例如文档说

<块引用>

  1. 两台计算机上的源代码中,添加以下几行,用适当的密码替换 my_secret 以进行身份​​验证远程调试,并用合适的IP替换地址地址(或本地主机)和端口号:

ptvsd.enable_attach("my_secret", address = ('0.0.0.0', 3000))

但我只有一份源文件副本,即在远程系统上.我刚刚将它安装在我的本地文件系统上.所以,我应该提供我的本地 ip 地址还是应该是远程系统 ip 以及我应该使用哪个端口号,因为我只有一份源副本,因此 ptvsd,它将如何进行通信.另外,在配置中,我应该为 localRoot 位置和 remoteRoot 位置使用什么.

 3. {"name": "附加(远程调试)",类型":蟒蛇","请求": "附加","localRoot": "${workspaceFolder}","remoteRoot": "${workspaceFolder}",端口":3000,"secret": "my_secret",主机":本地主机"}

感谢 VSCode 中的远程开发扩展,远程调试现在非常容易.

解决方案

你需要把远程ip地址同时放在ptvsd.enable_attach("my_secret", address = ('remote_ip_address', 3000))launch.json:

<代码>{"name": "附加(远程调试)",类型":蟒蛇","请求": "附加","localRoot": "${workspaceFolder}","remoteRoot": "/home/user1/scripts/",端口":3000,"secret": "my_secret",主机":remote_ip_address"}

您还需要将remoteRoot值更改为脚本在远程机器中所在目录的路径(例如/home/user1/scripts/).

最后,打开一个 ssh 连接:ssh -L 3000:localhost:3000,在远程机器上运行你的脚本,并在本地机器上连接调试器.

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.

解决方案

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.

这篇关于在 VSCode 中设置 Python 远程调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆