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 localhost as the IDE host name
  • Set a port, e.g.: 2345
  • Install pydevd-pycharm:
    pip install pydevd-pycharm~=<version of PyCharm on the local machine>
    
    You can find your PyCharm’s version from Help -> About, it’s something like #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 sudo
    sudo python -m your_module
    

That’s it!