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! :)
Subscribe to Blog updates
Thanks, we've got you!
Smart execution of R code
R plugin is announcing some helpful features to track execution of your R code: 1. Execute your R file as a runnable process, job. Jobs are shown in a separate tab in the R console. You can preview the job status (succeeded or failed), the duration of the execution, and the time you launched the …
New Database Features in PyCharm 2020.1
PyCharm 2020.1 comes with new Database export features that make exporting data easier. You can now see previews of the data that you want to export, letting you have a quick glance of what the data will look like. Furthermore, as many of our users prefer to export to Excel files, we've also adde…
R 4.0.0 and better UX in the R Plugin
Although it has not been a while since our last update release, we’re ready to announce some important enhancements and new features: Long-awaited support of R 4.0.0 . Download it and try with the R plugin for PyCharm. In response to your feedback, we improved user interface and workflows for…
New VCS Features in PyCharm 2020.1
The Version Control System support in PyCharm has been revamped with a new and improved commit workflow as well as an interactive rebasing option. This means, that it gives you more control over the way you commit your changes. New Commit Tool Window There are a couple of changes that are notewo…