Feature Spotlight: Python Debugger and ‘Attach to Process’

Happy Friday everyone,

Today I’d like to highlight some recent changes and improvements in PyCharm`s Python debugger and explore the brand new attach to process in more detail.

Previously PyCharm used a special fork of the PyDev debugger with some PyCharm-specific changes, as well as our own unique features like GEvent debugging and breakpoints at Django templates. Before releasing PyCharm 4, we decided to contribute to open-sourced software and merged our debugger code with that from the original PyDev repository. Now the work on the debugger continues from both sides (PyCharm and PyDev) in one place. The merged source code now lives at https://github.com/fabioz/PyDev.Debugger and pull requests are greatly appreciated!

As a result of the unification and the work done since PyCharm 3.4, the debugger in PyCharm 4 has grown more powerful. We’ve added the Referring Objects view, Jinja 2 templates debugging, and other small improvements. Still, the most notable debugger feature introduced in PyCharm 4 is attach to process.

Using this feature you can connect the PyCharm debugger with any running Python process and debug in the attached mode.

Note: ‘Attach to Process’ functionality is available in both PyCharm Community Edition and PyCharm Professional Edition starting with v4.0. This feature is supported on all Windows, Linux and Mac platforms.

Let me show you how it works.

Say we have some long running application that we want attach to. In my example I’ll use a simple graphical Minecraft-inspired application that I found on GitHub.

I have opened the project in PyCharm and launched it using standard run/debug configurations:


The project is running, so now I can attach the debugger to it. For that I go to Tools and select Attach to Process. PyCharm shows the list of running Python processes in our system. I select the Minecraft process and click OK:


PyCharm automatically shows the debugger console with the message that the debugger has successfully connected to the process:


From this point on, debugger works in the attached mode. Let’s make use of it and stop at some line of code. To do that, in the editor I put a breakpoint on the line with some reachable code. In my example I’ve put the breakpoint on the line containing the definition of the cube_vertices function, which is being called a lot. In a few seconds, once the process reaches the breakpoint, PyCharm will stop at the breakpoint and show us the debugger with frames, variables and watches views:


Now we can use the debugger as usual – stepping into/over, pausing and resuming the process, evaluating variables and expressions, and changing the runtime context:


That’s it! Hopefully you’ll appreciate this addition to the new joint-effort PyCharm debugger, along with other handy features introduced in the latest version of PyCharm.

And if you’re craving for yet more details on PyCharm debugger, see this tutorial or the online help.

See you next week,

About Dmitry Filippov

Product Marketing Manager at JetBrains
This entry was posted in Cool Feature. Bookmark the permalink.

16 Responses to Feature Spotlight: Python Debugger and ‘Attach to Process’

  1. Peter Shinners says:

    Alas, the Attach to Process requires a gdb version that is newer than what ships on Redhat 6.5. This ships with gdb 7.2, which does not work at all. I can’t find any documentation or information about what version of gdb is actually required.

  2. laike9m says:

    Well done, this is an amazing feature!

  3. sergio says:

    This is really nice. Will this ever be enhanced to work with remote Pythons, like in a Vagrant box?

  4. Nilesh Thali says:

    I’m using IDEA 15 Community Edition with the PyCharm Community Edition plugin. i can’t debug using the “debug” button in the toolbar (throws a NoSuchMethodError), and though i can attach to a process as you mentioned above, reloading the web page doesn’t end up stopping at any breakpoint.
    what am i doing wrong?

  5. PK says:

    I get the following error when I try to attach to a running python process. I am running Ubuntu 14.04, PyCharm Community Edition 4.5.2 and Python 2.7.x.

    Attaching to a process with PID=3863
    /usr/bin/python2.7 /home/pk/Downloads/pycharm-community-4.5.2/helpers/pydev/pydevd_attach_to_process/attach_pydevd.py –port 45482 –pid 3863
    Attaching with arch: i386:x86-64
    Running: gdb –nw –nh –nx –pid 3863 –batch –eval-command=’set scheduler-locking off’ –eval-command=’set architecture i386:x86-64′ –eval-command=’call dlopen(“/home/pk/Downloads/pycharm-community-4.5.2/helpers/pydev/pydevd_attach_to_process/attach_linux_amd64.so”, 2)’ –eval-command=’call DoAttach(0, “import sys;sys.path.append(\”/home/pk/Downloads/pycharm-community-4.5.2/helpers/pydev\”);sys.path.append(\”/home/pk/Downloads/pycharm-community-4.5.2/helpers/pydev/pydevd_attach_to_process\”);import attach_script;attach_script.attach(port=45482, host=\”\”);”, 0)’ –command=’/home/pk/Downloads/pycharm-community-4.5.2/helpers/pydev/pydevd_attach_to_process/linux/gdb_threads_settrace.py’
    Running gdb in target process.
    Attaching to a process with PID=3863
    stdout: [Thread debugging using libthread_db enabled]
    Using host libthread_db library “/lib/x86_64-linux-gnu/libthread_db.so.1”.
    0x00007f33406a7d83 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:81
    The target architecture is assumed to be i386:x86-64
    $1 = 47431744
    $2 = 34422944
    $3 = 0

    stderr: 81 ../sysdeps/unix/syscall-template.S: No such file or directory.
    Traceback (most recent call last):
    File “/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py”, line 63, in
    from libstdcxx.v6.printers import register_libstdcxx_printers
    ImportError: No module named ‘libstdcxx’

    Process finished with exit code 0
    Server stopped.

  6. Daniel Ross says:

    Is there any way to initiate such a connection programmatically from the other end? I work on a server application which spawns a hefty number of processes, and it’s very difficult if not impossible to determine which one I need to be connecting to this way. It would be much easier to use something along the lines of the “import pdb; pdb.set_trace()” pattern.

  7. Thomas St. Pierre says:

    Is there any news on attaching to remote python process? Doesn’t look like there’s any activity on https://youtrack.jetbrains.com/issue/PY-14702

  8. foo says:

    I love this, but if I don’t time the attach just right, I get this

    pthread_cond_wait: Invalid argument
    pthread_cond_wait: Invalid argument
    pthread_cond_wait: Invalid argument
    forever and process hangs

  9. Thomas Ward says:

    Does the debugger autoattach to subprocesses as well? I have a script that executes a python foo.py subprocess and I’d like to attach there too.

Leave a Reply

Your email address will not be published. Required fields are marked *