How to integrate debugpy

Hi,
I am experimenting with the attachable Python debugger of VSCode. In principle you write something like this at the beginning of your code:

import debugpy
debugpy.listen(8260)
debugpy.wait_for_client()
debugpy.breakpoint()

Then the debugger initializes, listens and waits on port 8260 until VSCode has connected to it. Then it stops at the breakpoint and you can debug your program through a SSH tunnel remotely.

Now I want to make it work with Plone. Say I have installed a zope2instance named client1. Where do I put that code now? I never dug so deep down to the starting process of an instance. Depending on which command I use (fg, console, debug, start, run) different things happen, sometimes processes were forked, sometimes the current process is replaced and sometimes a subprocess is going to be started. And codes and scripts were compiled at runtime and then executed.
I really wish there was a command like client1 debugpy 8260 which starts client1 in console mode and runs the few lines I mentioned before. That would be a nice extension for the zope2instance recipe.
However since there is no such command, how could I simulate that behaviour? Can I create a script which initializes debugpy and starts Plone normally, and then I run this script with client1 run <script>? If yes, how has this script to look like? I know how to write scripts which runs catalog updates or makes a quickinstall on every available Plone site but how can I start the service like fg or console does it?

Thank you!

I think I found the solution by myself:

from Zope2.Startup.serve import main
import sys

import debugpy
debugpy.listen(8260)
debugpy.wait_for_client()
debugpy.breakpoint()

main(
    argv=[sys.argv[0], '/home/ngoeddel/plone/zeocluster/parts/client1/etc/wsgi.ini']
)

If I now run client1 run script.py then after a few seconds I am able to connect to the remote debugger from within VSCode. After the click on the run button I then can set breaktpoints where I want, examine the global and local environments and step through my code. That's nice!

2 Likes

Plone Foundation Code of Conduct