说明:

在做深度学习项目时接到一个需求,客户做tensflow进行开发,把镜像传上去跑任务,由于限制,只能在镜像上运行,本地无法自测。虽然可以使用pycharm的远程调试。但是pycharm自带的远程调试无法自定义端口,我们镜像就不知道放哪个端口出去?好在pycharm提供Python Remote Debug。可以自定义端口。在Python Remote Debug的模式下,PyCharm(IDE)作为服务端(Server)启动调试,开发机则作为客户端(Client)启动。 使用 PyCharm 的 Python Remote Debug 进行远程调试,需要在本地开发环境中设定 IP 和端口并启动,PyCharm 会对设定的端口进行监听,等待客户端连接请求。

注意:由于开发机需要连接至本地开发环境,因此要保证开发机可以访问本地IP,可直接在开发机ping 本地ip验证。
步骤如下:

1:在想调试的开发机环境上安装 pydevd

pip3 install pydevd -i https://pypi.doubanio.com/simple/

2:本地PyCharm配置Python Remote Debug

Python Remote Debug配置如下:

  1. 【运行】->【编辑配置】->【点击+号】->【Python Remote Debug】
    填写Local host namePort,其中Local host name指的是本机开发环境的IP地址,而Port则随便填写一个端
    口即可(为了不冲突最好30000以上)。
  2. 配置 Path mappings,Local path 为项目在本地的位置,Remote path为项目在开发机的位置
  3. 【应用】并【确认】

    图1

3:在开发机需要调试的代码中加入调试监控代码, 将配置好的Python Remote Debug中的 Update your script 下方代码插入至开发机的代码中

# 192.168.10.173 替换成你本地本机的ip
import pydevd
pydevd.settrace('192.168.10.173', port=30000, stdoutToServer=True, stderrToServer=True)

注意在开发机源代码中插入两行调试监控代码,也需要在本地代码的相应位置中插入两行,否则远程调试断点会串行。

4:在待远程调试的代码中打好断点, 右上角选中配置好的Python Remote Debug,进行 Debug,启动并处于监听状态,在 Console 中显示如下

Starting debug server at port 30000
Use the following code to connect to the debugger:
import pydevd
pydevd.settrace('192.168.10.173', port=30000, stdoutToServer=True, stderrToServer=True)
Waiting for process connection...

5:然后在开发机上运行你的程序,你会发现可以调试了,在 Console 中显示如下

Starting debug server at port 30000
Use the following code to connect to the debugger:
import pydevd
pydevd.settrace('192.168.10.173', port=30000, stdoutToServer=True, stderrToServer=True)
Waiting for process connection...
Connected to pydev debugger (build 183.4886.43)

调试界面如下图所示
图2

Last modification:July 10th, 2020 at 10:44 am