Based heavily on this old guide by TimLinux http://linfiniti.com/2012/09/remote-debugging-qgis-plugins-using-pycharm/
- In PyCharm click menu "Run -> Edit Configurations" (Or click Edit Configurations in the dropdown in the toolbar)
- Click the plus to add a new "Python Remote Debug" config
- Name the config. Use port 53100 (Could probably be any high port)
- Add to the python code:
import sys
sys.path.append('/Applications/PyCharm.app/Contents/debug-eggs/pycharm-debug.egg')
import pydevd
pydevd.settrace('localhost', port=53100, stdoutToServer=True, stderrToServer=True)
- In PyCharm run the newly made Python Remote Debug configuration. The PyCharm console will print something like
Starting debug server at port 53100
Use the following code to connect to the debugger:
import pydevd
pydevd.settrace('localhost', port=53100, stdoutToServer=True, stderrToServer=True)
Waiting for process connection...
- Now activate/reload your plugin in QGIS.
If everything goes well your PyCharm console will now print
Connected to pydev debugger (build 143.1919.2)
Now PyCharm debugger will break at the line
pydevd.settrace('localhost', port=53100, stdoutToServer=True, stderrToServer=True)
As if it where a regular break point. Continuing from here just use PyCharm break points.
Note that Remote Debugging is supported in the Professional edition only.
https://www.jetbrains.com/help/pycharm/remote-debugging.html