python - 在 VSCode 中设置 Python 远程调试

标签 python visual-studio-code

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

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

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

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

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

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

  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))

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

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

编辑:由于 VSCode 中的远程开发扩展,现在远程调试非常容易。

最佳答案

您需要将远程 ip 地址放在 ptvsd.enable_attach("my_secret", address = ('remote_ip_address', 3000))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" 
}

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

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

关于python - 在 VSCode 中设置 Python 远程调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809545/

相关文章:

python - 如何对 numpy 数组进行 n 维距离和最近邻计算

c - VS Code 中的 inet_aton() 没有完成建议

visual-studio-code - Git Commits View 未显示在 VSCode 源代码控制面板中

c# dotnet CLI "error: There are no versions available for the package ' Newtonsoft.Json'。”

Python:for循环 - 在同一行打印

python - 在 odoo 12 中找不到模型

java - 如何将环境变量设置为localhost?

python - Tensorflow - Deep MNIST 教程 - 将分类器导出到 C++

c++ - 在 VS Code 中使用 Arduino 时,IntelliSense 会引发 #include 错误

visual-studio-code - 如何将用户设置传递给调试适配器?