如何使用 PyCharm 启动远程调试?
我正在尝试在 PyCharm(在 Windows 主机上)和运行我的 django 应用程序的 debian 虚拟主机之间进行调试。说明显示安装 Egg、添加导入,然后调用命令。我认为这些事情需要在 debian 主机上完成?
好吧,那么我应该把这两行放在哪个文件中呢?
from pydev import pydevd
pydevd.settrace('not.local', port=21000, stdoutToServer=True, stderrToServer=True)
我尝试将其放入 settings.py 中,但得到了这种东西......
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 只是坐在那里“等待连接”
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)
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
Whilst pycharm just sat there "waiting for connection"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
PyCharm(或您选择的 ide)充当“服务器”,您的应用程序充当“客户端”;因此,您首先启动服务器 - 告诉 IDE 进行“调试” - 然后运行客户端 - 这是一些带有
settrace
语句的代码。当您的 python 代码命中settrace
时,它会连接到服务器 - pycharm - 并开始向其提供调试数据。为了实现这一点:
1.将
pydev
库复制到远程计算机因此,我必须从
C:\Program Files\JetBrains\PyCharm 1.5.3\pycharm-debug.egg
复制文件code> 到我的 Linux 机器。我把它放在/home/john/api-dependancies/pycharm-debug.egg
2.将 Egg 放入 PYTHONPATH
希望您明白,除非 Python 能够找到它,否则您将无法使用 Egg。我想大多数人都使用 easy_install 但在我的例子中,我通过添加以下内容明确添加了它:
这只是必要的,因为我仍然没有成功安装鸡蛋。这是我的解决方法。
3.设置调试服务器配置
在 PyCharm 中,您可以通过以下方式配置调试服务器:
“确定”
本地主机名:表示服务器的名称 - 在我的例子中是 Windows 主机 - 或者实际上是 Windows 主机的 IP 地址(因为主机名)我的远程机器不知道。因此虚拟(远程)机必须能够访问主机。
ping
和netstat
对此很有用。端口:可以是您喜欢的任何空闲的非特权端口。例如:
21000
不太可能被使用。暂时不用担心路径映射。
4.启动调试服务器
将出现调试控制台选项卡,您应该进入
控制台,这意味着 ide 调试服务器正在等待您的代码打开与其的连接。
5.插入代码
这在单元测试中有效:
在 django Web 应用程序中,放置它的位置有点挑剔 - 似乎只有在完成其他所有操作后才能工作:
同样,IP 地址是您所在的框正在运行 Pycharm;您应该能够从运行代码/网站的框中 ping 该 IP 地址。端口是您的选择,只需确保您已告诉 pycharm 侦听同一端口即可。我发现
suspend=False
的问题比默认值要少,不仅会立即停止,因此您不确定它是否正常工作,而且还会尝试流式传输到 stdin/out,这也可能会给您带来悲伤。6.打开防火墙
默认情况下,Windows 7 防火墙会阻止您的传入连接。在远程主机上使用 netstat,您将能够看到 SYN_SENT 永远不会变为 ESTABLISHED,至少在您为应用程序“pycharm”的 Windows 防火墙添加例外之前不会。
OS/X 和 Ubuntu 没有防火墙(默认情况下,后来可能有人应用了防火墙)。
7.设置断点并运行代码
毕竟,当一切按计划进行时,您可以设置断点 - 在 settrace 运行后的某个地方 - 并且 pycharm 控制台将显示
,并且在“调试器”选项卡下变量堆栈将显示开始工作,您可以单步执行代码。
8.映射
告诉 pycharm 在哪里可以找到源代码。因此,当调试器说“我正在运行文件 /foo/bar/nang.py 的第 393 行时,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 thesettrace
it connects to the server - pycharm - and starts feeding it the debug data.To make this happen:
1. copy the
pydev
library to the remote machineSo I had to copy the file from
C:\Program Files\JetBrains\PyCharm 1.5.3\pycharm-debug.egg
to my linux machine. I put it at/home/john/api-dependancies/pycharm-debug.egg
2. Put the egg in the PYTHONPATH
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:
This is only necessary because I've still had no success installing an egg. This is my workaround.
3. setup the debug server config
In PyCharm you can configure the debug server via:
"OK"
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
andnetstat
are good for this.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. Start the debug server
The debug console tab will appear and you should get
in the console which means that the ide debug server is waiting for your code to open a connection to it.
5. Insert the code
This works inside a unit test:
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:
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. Open the firewall
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 and Ubuntu do not have firewalls to punch threw (by default, someone may have applied one later).
7. Set a breakpoint and run the code
After all that, when everything goes to plan, you can set a breakpoint - somewhere after the settrace has run - and pycharm console will show
and under the 'Debugger' tab the variables stack will start working and you can step through the code.
8. Mappings
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.
Done.
实际上,这只是一条注释,但包含一些可能节省时间的信息。
现在
pip install pydevd
在ubuntu和centos 6上都对我有用如果你想真正调试位于防火墙之类的远程服务器,你可以使用以下技巧:
这允许远程代码连接到在
localhost:8081
上侦听的计算机如果远程调试器确实这样做不想启动,说找不到套接字端口,检查你的防火墙规则。请注意,
127.0.0.1
规则与localhost
不同。It is just a note , actually , but contains some info that may save hours.
Right now
pip install pydevd
worked for me on both ubuntu and centos 6If you want to really debug remote server which is behind firewals and stuff, you can use the following trick:
This allows remote code to connect to your machine listening on
localhost:8081
If remote debugger does not want to start, saying it can't find socket port, check your firewall rules. Note that rule with
127.0.0.1
is not the same aslocalhost
.似乎由于某种原因,调试器无法使用 PyCharm 连接到您的 Windows 主机。您在 stderr 中没有收到任何其他消息吗?如果还没有,请尝试再运行一次,但使用 sterrToServer=false。这可能会显示它无法连接的真正原因。
It seems that for some reason debugger couldn't connect to your windows host with PyCharm. Haven't you got any other messages in stderr? If you have not, try to run it one more time, but with sterrToServer=false. That may show real reason why it doesn't connect.