Debugging Plone with VS Code and Python 2.7

Hi everyone,

I'm new to Plone and still learning Python, so please forgive me if I ask any basic question.

I'm trying to debug code using VS Code, and everything works fine until the server starts in a new process via a subprocess.call() command. I can set breakpoints in bin/initialize, but not in Zope-4.8.10-py2.7.egg/Zope2/Startup/serve.py.

The last point where I can set a breakpoint is at line 860 of plone.recipe.zope2instance-6.12.1-py2.7.egg/plone/recipe/zope2instance/ctl.py.

I'm working with Python 2.7, so I have to use the "Debugpy Old" extension since the "Python Debugger" no longer supports Python 2. Additionally, I'm using Dev Containers, so everything runs inside a Docker container.

Here is my launch configuration:

{
    "name": "bin/instance",
    "type": "debugpy-old",
    "request": "launch",
    "program": "${workspaceFolder}/bin/instance",
    "args": [
        "fg"
    ],
    "console": "integratedTerminal",
    "justMyCode": false,
    "subProcess": true
}

Here are the versions I'm working with:

  • Plone 5.2.14 (5222)
  • CMF 2.7.1
  • Zope 4.8.10
  • Python 2.7.18 (default, Apr 20 2020, 19:34:11) [GCC 8.3.0]
  • PIL 6.2.2 (Pillow)
  • WSGI: On
  • Server: waitress 1.4.4

Has anyone else attempted this and succeeded?

I've searched online for information on debugging Plone with VS Code and debugging subprocesses in Python, but I haven't found the solution yet.

Thanks for your help!

If you are new to Plone, why are you using such an outdated combination of Python 2.7 and Plone 4? Both completely outdated and unmaintained.

Hi Andreas,

I want to contribute to Senaite, a laboratory information system built on top of Plone. Unfortunately, the team behind this project is struggling to keep up with version updates and provide support for everyone using the software in production environments. Moving to Python 3 is an ongoing challenge, which is why the project still uses Plone 5.

Do you think moving to Python 3 would solve my issue with VS Code debugging?

If anyone is currently working with VS Code and is able to debug Plone, regardless of the version, I would really appreciate it if you could share how you manage it.

Thanks!

Emiliolario via Plone Community wrote at 2024-8-15 06:11 +0000:

...
... moving to Python 3 would solve my issue with VS Code debugging?

No. Regarding debugging, there is no major difference between
Python 2 and Python 3.

For Python debugging I use both Products.PDBDebugMode
and the Python debugger (pdb).

Products.PDBDebugMode enters the Python debugger when
Plone's request processing raises an (unhandled) exception
or logs an event at level at or above ERROR provided
that Plone runs in debug mode.

With the Python debugger, you can use code breakpoints.
A code breakpoint has the from import pdb; pdb.set_trace().
set_trace() enters the Python debugger.

The Python debugger allows you to inspect the state, single step,
move up and down the call chain.
It communicates via sys.stdin and sys.stdout.
To use the debugger, you must be able to send commands to sys.stdin
and read data sent to sys.stdout.

Your question mentions specifically VS code.
There ase special tools to debug VS code -- independent of Python.
But your example refers to Python code (specifically `serve?).
Thus, you might in fact be interested in Python debugging.

I never got Plone/Zope and VS Code working together as it should.
Best to use vi/vim or whatever commandline editor for debugging here :man_shrugging:

1 Like

Yes I got it working with vscode and 2.7.
From memory you can't use bin/instance due to the process call. I will dig out the python you have to run

I've come back to this issue today and managed to get it working.

For the record, here's what was missing:

I added the initialization of the debug server to my addon's main __init__.py:

from App.config import getConfiguration
import debugpy

if getConfiguration().debug_mode:
    debugpy.listen(5678)

I also added "debugpy" to the requirements in the Dockerfile for development containers.

With that in place, all you need to do is add the launch configuration to launch.json:

{
    "name": "Attach to Plone",
    "type": "debugpy-old",
    "request": "attach",
    "connect": {
        "host": "127.0.0.1",
        "port": 5678
    },
    "justMyCode": false
}

Start Plone with bin/instance fg and attach VSCode using the custom launcher.

You'll be able to dynamically set breakpoints and explore all the variables.

Thanks for the support!