Skip to content

VSCode Debugging for Frappe Python

Shivam Mishra edited this page Dec 19, 2018 · 2 revisions

screen shot 2018-10-08 at 12 52 10 pm

Checklist for proper functioning.

Caveats

  1. Disables Auto Reload Feature (However You can achieve the same results by manual reload (⌘⇧F5))
  2. Disables Werkzeug Multithreading

How To

1. Update Procfile

Caution: This modifies the behaviour of bench start

Comment out a line (prepend # to it) from the Procfile (located in the bench directory) that looks like this.

web: bench serve --port 8000

We will run this process from VS code instead of running it with bench start.

2. Update launch.json

Add a configuration to your launch.json in VS Code that should look something like this (This more or less does exactly what the removed line from Procfile does).

{
    "name": "Bench",
    "type": "python",
    "request": "launch",
    "program": "${workspaceFolder}/frappe/frappe/utils/bench_helper.py",
    "args": [
        "frappe", "serve", "--port", "8000", "--noreload", "--nothreading"
    ],
    "pythonPath": "${workspaceFolder}/../env/bin/python",
    "cwd": "${workspaceFolder}/../sites",
    "env": {
        "DEV_SERVER": "1"
    }
}

Paths mentioned in given configuration assumes that you have apps directory as your workspace directory (The directory you open code with). workspaceFolder is a vs code variable that points to (if it's not obvious from its name) workspace directory.

You are not forced to use apps as your workspace directory, however do remember to change workspaceFolder, pythonPath and cwd accordingly.

3. Execute bench start

This should be kept running as usual.

4. Start debugging

VS Code -> Debug Panel (⌘⇧D) -> Start Debugging or With a keyboard shortcut(⌘⇧F5).

Explanation

1. program and args

"program": "${workspaceFolder}/frappe/frappe/utils/bench_helper.py",
"args": ["frappe", "serve", "--port", "8000", "--noreload", "--nothreading"],

Does exact same thing as bench serve --port 8000 --noreload --nothreading which is same as

cd sites
../env/bin/python ../apps/frappe/frappe/utils/bench_helper.py frappe serve  --port 8000 --noreload --nothreading

--noreload diasbles werkezeug's autoreload fetaure and --nothreading disables multithreading.

2. cwd

"cwd": "${workspaceFolder}/../sites",

Above command must be executed from sites directory.

3. env

"env": {
    "DEV_SERVER": "1"
}

bench start creates an environment variable DEV_SERVER and set it to 1. Socket.io doesn't work correctly without this (long story).

Running only bench serve doesn't set this variable so you need to explicitly set it.

Details

Currently, frappe runs with use_reloader=True and threaded=True, VS Code Debugger for some reason doesn't play well with these features, Django and Flask also have this problem.

Clone this wiki locally