PyCharm: Debug script or module with sudo privileges
I was trying to debug a python module using PyCharm, however the module requires sudo to run and it seems it’s not possible to set a custom python command to be run in the default interpreter configuration.
I tried following different approaches (see: here and here), but they were either not working, or just cumbersome to setup.
Disclaimer: this approach requires PyCharm Professional
We’ll be using a local Python Debug Server for this approach
- Create a new Python remote debug server configuration 1
- Leave
localhostas the IDE host name - Set a port, e.g.:
2345 - Install
pydevd-pycharm:You can find your PyCharm’s version from Help -> About, it’s something likepip install pydevd-pycharm~=<version of PyCharm on the local machine>#PY-<version>. The install command can also be copied from the remote server configuration dialog. - Add this to your main (can be copied from the remote server configuration dialog as well):
pydevd_pycharm.settrace('localhost', port=2345, stdoutToServer=True, stderrToServer=True) - Start the debug server and set breakpoints
- From a terminal, start your script with
sudosudo python -m your_module
That’s it!
Read other posts