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:
- a project that you develop locally in PyCharm
- 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)
- 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! :)