Python Remote Debug with PyCharm

Recent PyCharm 1.1 EAP has added one of the most voted feature — Remote Debug. I’m going to say a few words about it expecting you to try it and share your thoughts with us.

If you plan to debug something “remote” it means that you have:

  1. a project that you develop locally in PyCharm
  2. the same project at some remote host (note that sources really should be the same, otherwise you will see some strange things happening during debug session, e.g. inability to set breakpoints or seeing current execution point different from actual one)
  3. local host accessible from the remote one by socket connection

In order to use remote debug you need to perform only 3 steps:

Step 1: Start debug server

To do this you can use Python Remote Debug configuration

There you can just specify local host name, by which your local host is accessible from the remote one (192.168.56.1 in my case), and a port number for server to start at. Then launch this configuration.

Remote debug server is started at port 51234.

Step 2: Copy pycharm-debug.egg to your remote host

pycharm-debug.egg file is located in root of your PyCharm installation directory. Copy it to the remote host and add it to Python path.

Step 3: Update script to start debugger client

Insert 2 lines of code to your script to start debugger client that will connect to server and trace your code. These lines are pretty simple and you can copy them from the Remote Debug configuration dialog:

from pydev import pydevd

pydevd.settrace(’192.168.56.1′, port=51234, stdoutToServer=True, stderrToServer=True)

And that’s all! Just run your script and it connects to the server immediately going to suspend mode.

One last thing for you to take care: PyCharm needs to know how sources from remote host are mapped to your local project sources.
Thats why in case locations are different, you should provide path mappings.
For example if you have local project something like: C:ProjectsSuperProjectmega_script.py
and remotely: /home/testing/mega_script.py
you should enable ‘Use path mappings’ option in remote debug run configuration settings and fill prefixes

Luckily PyCharm provides some useful means by setting file mappings during the process of debug if it wasn’t configured before.

If a file that should be debugged remotely is not found locally, you will see a special editor:

This editor gives you 3 options:
1) Chose ‘Edit settings’ to edit mappings once again in run configuration settings dialog
2) Press ‘Auto-detect’ to resolve mappings automatically
This will offer you to choose a file with the same name from your project and then automatically fix path mappings in your run configuration.

3) And in case you don’t have such a file in your project you are still able to view the source code if you download it from the remote host by pressing ‘Download’ link. But note that the file won’t be saved in project and will disappear along with termination of the debug session.

That’s it for now! We really want to know your thoughts on this functionality to make it work perfect for you. Leave your comments here or on PyCharm discussion forum.

Wishing you no bugs in your code! :)

This entry was posted in Cool Feature and tagged , . Bookmark the permalink.

12 Responses to Python Remote Debug with PyCharm

  1. Pingback: Tweets that mention JetBrains PyCharm Blog » Blog Archive » Python Remote Debug with PyCharm -- Topsy.com

  2. We’re hungry for new builds :-) It would be nice to have a schedule for it. I.e. every week on Friday evening. Please don’t do it more often or I will spend all my time re-installing the IDE :-/ With the amount of bugs that get fixed and features that make in it’s impossible to resist the temptation of trying it out in realtime…

  3. Robert says:

    Try a Mac…it is just drag-n-drop to install. :-)

  4. Robert, please don’t get me even started on this. In as much as I appreciate eye-candy and ergonomic design, graphic goodness and sleek interface, the lack of native package management (don’t even dare to mention Fink and MacPorts or it will provoke another burst of flames), primitiveness of the native terminal emulator, weird POSIXy userland, flaky X and all the rest just make it impractical for any half serious systems programming (high-level Python stuff is not the only thing I use the computer for).

    The reason why it takes time for me to re-install PyCharm is that I am mostly interested in bug fixes regarding the stub generation, so first I have to invalidate caches, then remove the relevant parts of the profile and only then unpack the new version + wait for half an hour for all my stubs to regenerate. The overhead has nothing to do with the process of unpacking the archive per se.

  5. Andru says:

    I’m new in Python. Sorry, why same python scripts named *.egg? Thank you!

  6. Tomas says:

    Nice. Worked with simple example. When I tried to debug celeryd worker I got this error:

    File “/home/tcirip/Development/celery/env-2.6.6/lib/python2.6/site-packages/celery/utils/__init__.py”, line 322, in instantiate
    return get_cls_by_name(name)(*args, **kwargs)
    File “/home/tcirip/Development/celery/env-2.6.6/lib/python2.6/site-packages/celery/apps/worker.py”, line 95, in __init__
    self._isatty = sys.stdout.isatty()
    AttributeError: IORedirector instance has no attribute ‘isatty’

    sys.out is pydev.pydevd_io.IORedirector has not isatty() implemented.

    Any suggestions?

    Tomas

  7. John says:

    using django-celery I am finding the the worker is lost

  8. Dima Q says:

    I have a bunch of long-running python processes deployed. Is there are a ready-made solution to include the debug server/agent into my process and have pycharm connect to the agent later?

    For now I’ll probably hack up some socket listener that starts debug agent when it receives a connection on magic port, or perhaps a magic signal.

  9. Jay Martin says:

    I’m a noob – been developing a django app that will need to catch a PayPal IPN message so I think I need to start the django app in apache (vs debug) and use remote debugging. I have never deployed a django app so that may be what I don’t understand about the instructions above, but what exactly does step 3 above “Update script to start debugger client” mean? What script?

  10. Ken says:

    How would this work over ssh? Do you have to setup ssh tunneling?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>