介绍下在eclipse中使用pydev插件进行远程调试,供参考。
先看下官网的资料,后面有测试总结:
Remote Debugger
In PyDev you can debug a remote program (a file that is not launched from within Eclipse).
The steps to debug an external program are:
- Start the remote debugger server
- Go to the debug perspective
- Start the external program with the file ‘pydevd.py’ in its pythonpath
- Call pydevd.settrace()
Let’s see a simple ‘step-by-step’ example on how this works:
1. Start the remote debugger server: To start the remote debugger server, you have to click the green button pointed by ‘1’ in the image below. After doing that, it will show a message in the console (indicated by ‘2’) to confirm that the server is listening for incoming connections.
Note: Those buttons should be present at the debug perspective and they can be enabled in other perspectives through Window > Customize perspective > Command groups availability > PyDev debug.
Image: Remote Debugger Server
2. Go to the debug perspective: This is needed because it has no actual ‘signal’ that it reached a breakpoint when doing remote debugging. So, if you already have it open, just cycle to it with Ctrl+F8. Otherwise, go to the menu: window > Open Perspective > Other > Debug.
Note that there should appear a process named ‘Debug Server’ in the debug view (see ‘1’ in the image below).
Image: Debug perspective
3. Make sure pydevd.py is in your pythonpath: This file is included in the org.python.pydev plugin. So, you’ll have to add it to the pythonpath. It’s exact location will depend upon the eclipse location and the plugin version, being something like:
eclipse/plugins/org.python.pydev_x.x.x/pysrc/pydevd.py
(so, the container folder must be in your pythonpath). If you choose to execute it from another machine, you need to copy all the files within that folder to the target machine in order to be able to debug it (if the target machine does not have the same paths as the client machine, the file pydevd_file_utils.py must be edited to properly translate the paths from one machine to the other – see comments on that file).
4. Call pydevd.settrace(): Now that the pydevd.py module is already on your pythonpath, you can use the template provided: ‘pydevd’ to make the call: import pydevd;pydevd.settrace(). When that call is reached, it will automatically suspend the execution and show the debugger.
Image: pydevd.settrace called
Important Notes
NOTE 1: the settrace() function can have an optional parameter to specify the host where the remote debugger is listening. E.g.:pydevd.settrace(‘10.0.0.1’)
NOTE 2: the settrace() function can have optional parameters to specify that all the messages printed to stdout or stderr should be passed to the server to show. E.g.: pydevd.settrace(stdoutToServer=True, stderrToServer=True)
NOTE 3: You can have the running program in one machine and PyDev on another machine, but if the paths are not exactly the same, some adjustments have to be done in the target machine:
Aside from passing the files in eclipse/plugins/org.python.pydev_x.x.x/pysrc to your target machine, the file pydevd_file_utils.py must be edited to make the path translations from the client machine to the server machine and vice-versa. See the comments on that file for detailed instructions on setting the path translations.
我在看完上面的资料后还是很疑惑,感觉没说清楚。 经过使用测试后总结下经验,使用中大家需注意以下四个问题:
1. eclipse端是debug服务端 , 即端口的监听端, 也就是说远端要调试的python代码文件是作为客户端连接eclipse端的端口(如:默认端口 5678), 切记!!我在这里吃了大亏。
2. 在eclipse插件plugins目录查找到pydevd.py文件后,将文件所在的整个个文件夹copy到需要调试的机器的python文件的site-packages目录下,并保证可以访问到pydevd.py(我使用了在site-packages目录下建立pysrc.pth文件指定路径方式),可参考文章最后的url。
3. 如果远程调试,一般python代码的路径在eclipse端与远程机器上的路径是不一样的,一定记住修改pydevd_file_utils.py中的PATHS_FROM_ECLIPSE_TO_PYTHON值,修改具体值可以看下文件中关于这个变量的注释,而且注释中还有举例说明。
4. 在需要调试的代码中使用下面代码:
import pydevd
pydevd.settrace(‘eclipse端服务器地址‘, port=端口5678, stdoutToServer=True, stderrToServer=True)
官网参考: http://www.pydev.org/manual_adv_remote_debugger.html
其它参考:http://blog.csdn.net/tantexian/article/details/47003385