Python Remote Debug with PyCharm

Posted on by Dmitry Trofimov

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! 🙂

Comments below can no longer be edited.

22 Responses to Python Remote Debug with PyCharm

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

    December 17, 2010

    […] This post was mentioned on Twitter by Mathieu D., JetBrains PyCharm. JetBrains PyCharm said: [blog] Python Remote Debug with PyCharm http://bit.ly/hH186Q […]

  2. Yury V. Zaytsev says:

    December 17, 2010

    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:

    December 17, 2010

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

  4. Yury V. Zaytsev says:

    December 19, 2010

    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.

    • Sasha says:

      December 5, 2019

      Well said! Agree with every point. For me *nix all the way. I do openbsd, freebsd, linux and it works… Still, fancy tool pycharm, but heavy, slow at times, buggy too…
      overall I like it as it does do a solid job …

  5. Andru says:

    January 7, 2011

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

  6. Tomas says:

    February 11, 2011

    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

    • traff says:

      February 14, 2011

      Hi Tomas, that is a bug. I’ve created an issue for that http://youtrack.jetbrains.net/issue/PY-2933
      It will be fixed in future pycharm update.

    • John says:

      November 18, 2011

      Did you get the remote debugger working on celery?

  7. John says:

    June 9, 2011

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

  8. Dima Q says:

    April 5, 2012

    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:

    June 25, 2012

    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:

    August 27, 2013

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

  11. sgb says:

    June 29, 2015

    from you dev machine run ssh root@remote_machine -R 4444:localhost:4444 -N. this would setup a tunnel.

  12. Barney Gale says:

    March 2, 2016

    Has this feature been moved to paid-only?

    • Dmitry Trofimov says:

      March 2, 2016

      Hi, Barney. All features related to remote development are only available in PyCharm Professional Edition.

  13. Sanjeev Johal says:

    December 8, 2017

    I got this working whereby I was using expandrive to mount a linux remote Django project to a windows mapped drive but then Pycharm would keep regenerating skeletons and re-indexing.

    So now have the Django project local and then deployed to linux server but now everytime I start debug and set a breakpoint thereafter it immediately jumps to the next line after my “import pydevd” statement and in the debugger window have a lot of path mappings that need applying.

    Whereas the behaviour I was getting before was I would navigate to the Django admin page and would pause at the breakpoint giving me all the debugger information.

  14. Sanjeev Johal says:

    December 8, 2017

    I got this working whereby I was using expandrive to mount a linux remote Django project to a windows mapped drive but then Pycharm would keep regenerating skeletons and re-indexing.

    So now have the Django project local and then deployed to linux server but now everytime I start debug and set a breakpoint thereafter it immediately jumps to the next line after my “import pydevd” statement and in the debugger window have a lot of path mappings that need applying.

    Whereas the behaviour I was getting before was I would navigate to the Django admin page and would pause at the breakpoint giving me all the debugger information.

    PS. This now only works whenever I restart all services using supervisor

  15. Отладка проекта в PyCharm - python pycharm pydev - Вопросы и ответы по программированию says:

    December 15, 2018

    […] Решил попробовать в обход — использовать удаленную отладку — запустить на локальном ПК скрипт от нужного пользователя и приотачиться к нем PyCharm’ом (опять же, не нашел типичной для VS опции «присоединиться к процессу»). Настройку делал в соответствии с руководством https://blog.jetbrains.com/pycharm/2010/12/python-remote-debug-with-pycharm/ […]

  16. Roman Kazinnik says:

    April 4, 2019

    PyCharm will fail to import python files in debugging using docker containers.

    Try this simple repo: add as 1st line in any main.py
    import file.py

    PyCharm will fail to map/download/locate “file.py” !

  17. Jonathan says:

    June 5, 2019

    I would love the remote debug listener to start on PyCharm startup, but because it’s a ‘debug’ configuration, I cannot add it to startup.
    I’ve tried using a compound config to start the debug session, but that wouldn’t work either.

    Any suggestions on how to start the python remote debug session on PyCharm startup?

  18. Marko says:

    February 3, 2020

    How to reset the setting for “auto-detect source” and “download source” if we pressed the “don’t ask again” button?

Subscribe

Subscribe for updates