如何使用 PyCharm 启动远程调试?

编程入门 行业动态 更新时间:2024-10-11 13:22:03
本文介绍了如何使用 PyCharm 启动远程调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在 PyCharm(在 Windows 主机上)和运行我的 django 应用程序的 debian 虚拟主机之间进行调试.说明说安装 egg,添加导入,然后调用命令.我认为这些事情需要在 debian 主机上完成?

I'm trying to get debugging up between PyCharm (on windows host) and a debian virtual host running my django application. The instructions say to install the egg, add the import, and then invoke a command. I assume these things need to be done on the debian host?

好的,那么,我应该把这两行放在哪个文件中?

Ok, then, in what file should I put these two lines?

from pydev import pydevd pydevd.settrace('not.local', port=21000, stdoutToServer=True, stderrToServer=True)

我尝试将其放入 settings.py 中,但得到了这种东西......

I tried putting it into the settings.py but got this kind of thing...

File "/django/conf/__init__.py", line 87, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "/django/utils/importlib.py", line 35, in import_module __import__(name) File "/settings.py", line 10, in <module> pydevd.settrace('dan.local', port=21000, stdoutToServer=True, stderrToServer=True) File "/pycharm-debug.egg/pydev/pydevd.py", line 1079, in settrace debugger.connect(host, port) File "/pycharm-debug.egg/pydev/pydevd.py", line 241, in connect s = StartClient(host, port) File "/pycharm-debug.egg/pydev/pydevd_comm.py", line 362, in StartClient sys.exit(1) SystemExit: 1

虽然 pycharm 只是坐在那里等待连接"

Whilst pycharm just sat there "waiting for connection"

推荐答案

PyCharm(或您选择的 ide)充当服务器",您的应用程序是客户端";所以你首先启动服务器 - 告诉 IDE调试" - 然后运行客户端 - 这是一些带有 settrace 语句的代码.当您的 python 代码点击 settrace 时,它会连接到服务器 - pycharm - 并开始为其提供调试数据.

PyCharm (or your ide of choice) acts as the "server" and your application is the "client"; so you start the server first - tell the IDE to 'debug' - then run the client - which is some code with the settrace statement in it. When your python code hits the settrace it connects to the server - pycharm - and starts feeding it the debug data.

要做到这一点:

1.将 pydev 库复制到远程机器上

1. copy the pydev library to the remote machine

所以我不得不将文件从 C:Program FilesJetBrainsPyCharm 1.5.3pycharm-debug.egg 复制到我的 linux 机器上.我把它放在 /home/john/api-dependancies/pycharm-debug.egg

So I had to copy the file from C:Program FilesJetBrainsPyCharm 1.5.3pycharm-debug.egg to my linux machine. I put it at /home/john/api-dependancies/pycharm-debug.egg

2.将鸡蛋放在 PYTHONPATH 中

希望您明白,除非 python 可以找到它,否则您将无法使用 egg.我猜大多数人都使用easy_install,但在我的例子中,我明确地添加了它:

Hopefully you appreciate that you're not going to be able to use the egg unless python can find it. I guess most people use easy_install but in my instance I added it explicitly by putting this:

import sys sys.path.append('/home/john/app-dependancies/pycharm-debug.egg')

这只是必要的,因为我仍然没有成功安装鸡蛋.这是我的解决方法.

This is only necessary because I've still had no success installing an egg. This is my workaround.

3.设置调试服务器配置

在 PyCharm 中,您可以通过以下方式配置调试服务器:

In PyCharm you can configure the debug server via:

  • 运行-> 编辑配置:打开运行/调试配置"对话框
  • 默认值 -> Python Remote Debug":是要使用的模板
  • 填写本地主机名和端口,您可能想要使用路径映射",但更多内容请参见下文...
  • 好的"

  • Run-> Edit Configurations: opens the 'Run/Debug Configurations' dialog
  • Defaults -> "Python Remote Debug": is the template to use
  • fill out the local host name and port and you'll probably want to 'use path mapping' but more on all this below...
  • "OK"

本地主机名:表示服务器的名称——在我的例子中是windows主机——或者实际上是windows主机的IP地址,因为主机名我的远程机器不知道.所以虚拟(远程)机器必须能够到达主机.ping 和 netstat 对此有好处.

Local host name: means the name of the server - that's the windows host machine in my case - or actually the IP Address of the windows host machine since the hostname is not known to my remote machine. So the virtual (remote) machine has to be able to reach the host. ping and netstat are good for this.

端口:可以是您喜欢的任何空闲的非特权端口.eg: 21000 不太可能被使用.

Port: can be any vacant non-priviledged port you like. eg: 21000 is unlikely to be in use.

暂时不用担心路径映射.

Don't worry about the path mappings for now.

4.启动调试服务器

  • 运行-> Debug : 启动调试服务器 - 选择你刚刚创建的配置.
  • Run-> Debug : start the debug server - choose the configuration you just created.

调试控制台选项卡将出现,你应该得到

The debug console tab will appear and you should get

Starting debug server at port 21000

在控制台中,这意味着 ide 调试服务器正在等待您的代码打开与它的连接.

in the console which means that the ide debug server is waiting for your code to open a connection to it.

5.插入代码

这在单元测试中有效:

from django.test import TestCase class APITestCase(TestCase): def test_remote_debug(self): import sys sys.path.append('/home/john/dependancies/pycharm-debug.egg') from pydev import pydevd pydevd.settrace('192.168.33.1', port=21000, suspend=False) print "foo"

在 django web 应用程序中,放置它的位置有点挑剔 - 似乎只有在完成其他所有操作后才能工作:

And in a django web application it's a bit finicky about where you put it - seems to work only after everything else is done:

if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv) sys.path.append('/vagrant/pycharm-debug.egg') import pydevd pydevd.settrace('192.168.33.1', port=21000, suspend=False)

IP 地址就是你运行 Pycharm 的地方;您应该能够从运行您的代码/网站的框中 ping 该 IP 地址.端口是你的选择,只要确保你告诉 pycharm 监听同一个端口.而且我发现 suspend=False 的问题比默认设置要小,不仅会立即停止,因此您不确定它是否正常工作,而且还尝试流式传输到 stdin/out,这也可能让您感到悲伤.

Again that the IP address is the box where you're running Pycharm on; you should be able to ping that ip address from the box running your code/website. The port is your choice, just make sure you've told pycharm to listen on the same port. And I found the suspend=False less problematic than the defaults of, not only immediately halting so you're not sure if it's working, but also trying to stream to stdin/out which might give you grief also.

6.打开防火墙

默认情况下,Windows 7 防火墙会阻止您的传入连接.在远程主机上使用 netstat,您将能够看到 SYN_SENT 永远不会变为 ESTABLISHED,至少在您为应用程序 'pycharm' 向 Windows 防火墙添加例外之前不会.

Windows 7 firewall will, by default, block your incoming connection. Using netstat on the remote host you'll be able to see that SYN_SENT never becomes ESTABLISHED, at least not until you add an exception to the windows firewall for the application 'pycharm'.

OS/X 和 Ubuntu 没有防火墙可打(默认情况下,有人可能会稍后应用).

OS/X and Ubuntu do not have firewalls to punch threw (by default, someone may have applied one later).

7.设置断点并运行代码

毕竟,当一切按计划进行时,您可以设置一个断点 - 在 settrace 运行后的某个地方 - pycharm 控制台将显示

After all that, when everything goes to plan, you can set a breakpoint - somewhere after the settrace has run - and pycharm console will show

Connected to pydev debugger (build 107.386)

在调试器"选项卡下,变量堆栈将开始工作,您可以单步执行代码.

and under the 'Debugger' tab the variables stack will start working and you can step through the code.

8.映射

Mapping 告诉 pycharm 在哪里可以找到源代码.因此,当调试器说我正在运行文件/foo/bar/nang.py 的第 393 行时,Pycharm 可以将该远程绝对路径转换为绝对本地路径......并向您展示源代码.

Mapping tell pycharm where it can find the source code. So when the debugger says "i'm running line 393 of file /foo/bar/nang.py, Pycharm can translate that remote absolute path into an absolute local path... and show you the source code.

/Users/john/code/app/ /opt/bestprice/app/ /Users/john/code/master/lib /opt/bestprice/lib/python2.7/site-packages

完成.

更多推荐

如何使用 PyCharm 启动远程调试?

本文发布于:2023-11-25 20:29:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631238.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   PyCharm

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!