VS Code launch.json for debugging runwsgi

Here's a launch.json I got working to debug a Plone Classic add-on that uses runwsgi (you will probably need to change the PYTHONPATH value that I used, in the last line):

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch runwsgi with debugpy",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/.venv/bin/runwsgi",
      "args": [
        "instance/etc/zope.ini"
      ],
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}",
      "justMyCode": false,
      "subProcess": true,
      "preLaunchTask": "activate-venv",
      "env": {
        "PYTHONPATH": "${workspaceFolder}/.venv/lib/python3.10/site-packages"
      }
    }
  ]
}

It uses task.json to define the activate-venv task. You will no doubt need to change the path to activate.

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "activate-venv",
            "type": "shell",
            "command": "source .venv/bin/activate",
            "options": {
                "cwd": "${workspaceFolder}"
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared",
                "showReuseMessage": false,
                "clear": false
            }
        }
    ]
}

It shouldn’t be necessary to activate the virtualenv, since you’re giving the full path to the binary you’re running rather than expecting to find it in $PATH.

1 Like

I would assume that the activated python interpreter/venv) is also used for the launch.

Asked AI to check, it suggests to use this in the launch.json for python and omit the PYTHON_PATH

  // Use the interpreter VS Code already selected for this workspace:
  "python": "${command:python.interpreterPath}",

You worked on your solution because something wasn’t working for you on your setup @tkimnguyen What were the shmptoms?

I’ve had too many struggles with VS Code already, so when I see something nee I’d like to understand what’s going on. :flushed_face:

Edit: Python2.7 support maybe?

1 Like

Thanks! You were right.

At some point in this particular "journey" I thought it was needed because for a while Python wasn't finding Zope.

The task.json file is no longer needed either.

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch runwsgi with debugpy",
      "type": "debugpy",
      "request": "launch",
      "program": "${workspaceFolder}/.venv/bin/runwsgi",
      "args": [
        "instance/etc/zope.ini"
      ],
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}",
      "justMyCode": false,
      "subProcess": true,
      "env": {
        "PYTHONPATH": "${workspaceFolder}/.venv/lib/python3.10/site-packages"
      }
    }
  ]
}

Aw, you guys are so nice :slight_smile:

Yup! Again, I had explicitly specified PYTHON_PATH because of some trauma I suffered in this journey...

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Launch runwsgi with debugpy",
      "type": "debugpy",
      "request": "launch",
      "python":"${command:python.interpreterPath}",
      "program": "${workspaceFolder}/.venv/bin/runwsgi",
      "args": [
        "instance/etc/zope.ini"
      ],
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}",
      "justMyCode": false,
      "subProcess": true,
    }
  ]
}

I just wanted a run configuration (PyCharm speak), and even with the supplied docs it was clear as mud. The chat thing helped, in a very, very roundabout way. It was like asking a drunk person to help me code... unlikely to work at all.

Nope, this is Plone 6.1.3 on Python 3.10.

Thanks for this. I'm unfortunately trying to move from PyCharm to VSCode after several years as well.

This is all I needed for mine:

"configurations": [
        {
            "name": "Python Debugger: Module",
            "type": "debugpy",
            "request": "launch",
            "module": "Zope2.Startup.serve",
            "console": "integratedTerminal",
            "args": [
                "etc/zope.ini"
            ],
        }
    ]

I don't think using module over program matters here, that's just how I had it setup in Pycharm so I copied it over.

I would be curious if anyone has successfully used python.analysis.nodeExecutable to get around the fact that pylance crashes hard when one tries to open a source file in your eggs/omelette? Presumably, the 32-bit pointer compression that the bundled V8 that comes with VSCode is the barrier, and running an external node process for pylance to run may avoid this but consume more memory; either way, there’s a lot of symbols to load for all of Plone to be available to the language server.

Thus far, I have just restarted vscode after running into this, or avoided opening the Pandora’s Box of opening any upstream file from Plone itself (by opening things outside my add-ons in vim). At some point, I will try to run pylance in an external nvm-managed node process, but at the moment, I am still limping along.

@tkimnguyen Is this working? If so, do you mind if I add this to cookieplone-templates?

@ericof I gave up on VS Code. Maybe what I use often in PyCharm is available in VS Code, but I wasn't able to find it, and I just wanted to get stuff done. Maybe one day I will go back and try again.

1 Like