如何使用PyCharm启动远程调试? [英] How do I start up remote debugging with PyCharm?

查看:175
本文介绍了如何使用PyCharm启动远程调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在PyCharm(在Windows主机上)和运行我的django应用程序的debian虚拟主机之间进行调试。说明说要安装egg,添加import,然后调用一个命令。我假设这些事情需要在debian主机上完成?



那么,那么在什么文件中应该放这两行?

从pydev导入pydevd 
pydevd.settrace('not.local',port = 21000,stdoutToServer = True,stderrToServer = True)

我试着把它放入settings.py,但是得到了这样的东西...

 文件/django/conf/__init__.py,第87行,__init__ 
mod = importlib.import_module(self.SETTINGS_MODULE)
文件/django/utils/importlib.py,第35行,import_module
__import __(name)
文件/settings.py,第10行,< module>
pydevd.settrace('dan.local',port = 21000,stdoutToServer = True,stderrToServer = True)
文件/pycharm-debug.egg/pydev/pydevd.py,第1079行, settrace
debugger.connect(host,port)
文件/pycharm-debug.egg/pydev/pydevd.py,第241行连接
s = StartClient(host,port)
文件/pycharm-debug.egg/pydev/pydevd_comm.py,行362,在StartClient
sys.exit(1)
SystemExit:1

虽然pycharm坐在那里等待连接

解决方案

PyCharm(或您的首选)充当服务器,您的应用程序是客户端;所以你首先启动服务器 - 告诉IDE调试 - 然后运行客户端 - 这是一些代码,其中包含 settrace 语句。当你的python代码点击 settrace 它连接到服务器 - pycharm - 并开始给它调试数据。





1。将 pydev 库复制到远程机器



所以我必须从 C:\Program Files\JetBrains\PyCharm 1.5.3\pycharm-debug.egg 到我的linux机器。我把它放在 /home/john/api-dependancies/pycharm-debug.egg



2 。把蛋放在PYTHONPATH



希望你明白,除非python可以找到它,否则你不能使用蛋。我想大多数人使用easy_install,但在我的实例中,我通过以下方式明确地添加了它:

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

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



3。设置调试服务器配置



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




  • 运行 - > 编辑配置:打开运行/调试配置对话框

  • - >Python远程调试:是使用的模板

  • 填写本地主机名和端口,您可能想要使用路径映射,但更多

  • OK



    本地主机名:意思是服务器的名称 - 这是我的情况下的Windows主机,或实际上是Windows主机的IP地址,因为我的远程机器不知道主机名。所以虚拟(远程)机器必须能够到达主机。 ping netstat 对此很有帮助。



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



    现在不要担心路径映射。 / p>




4。启动调试服务器




  • 运行 - > 调试:start调试服务器 - 选择刚刚创建的配置。



调试控制台选项卡将出现,您应该获得

 在端口21000启动调试服务器

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



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网络应用程序中,有一点关于你把它放在哪里 - 似乎工作只有在其他事情完成之后:

 如果__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,su花费=假)

再次,IP地址是您运行Pycharm的框;您应该能够从运行代码/网站的框中ping该IP地址。该端口是您的选择,只需确保您已经告诉pycharm在同一个端口上侦听。而且我发现 suspend = False 比默认值更少的问题,不仅立即停止,所以你不知道它是否正常工作,但也试图流入stdin / out这可能会让你感到悲伤。



6。打开防火墙



默认情况下,Windows 7防火墙将阻止您的传入连接。在远程主机上使用netstat,您可以看到SYN_SENT永远不会成为ESTABLISHED,至少直到您为应用程序pycharm的Windows防火墙添加异常。



OS / X和Ubuntu没有防火墙打倒(默认情况下,有人可能已经申请了一个)。



7。设置断点并运行代码



在所有这些之后,当一切都进行计划时,您可以设置一个断点 - 在settrace运行之后的某个地方 - pycharm控制台将显示

 连接到pydev调试器(build 107.386)

在Debugger选项卡下,变量堆栈将开始工作,您可以逐步完成代码。



8。映射



映射告诉pycharm可以在其中找到源代码。所以当调试器说我正在运行文件/foo/bar/nang.py的第393行时,Pycharm可以将该远程绝对路径转换为绝对本地路径...并向您显示源代码。

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

完成


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"

解决方案

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.

To make this happen:

1. copy the pydev library to the remote machine

So 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:

   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. setup the debug server config

In PyCharm you can configure the debug server via:

  • 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"

    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.

    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

  • 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

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:

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"

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)

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

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

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

Done.

这篇关于如何使用PyCharm启动远程调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆