VScode Debugger Not Working with Flask-Script Commad

117 Views Asked by At

I have a command created with flask-script and I want to debug it with VSCode debugger.

But when I run the VScode Debugger configured for Flask, it simply executes the command without stopping at the breakpoint.

But, when I specify breakpoint() in the code, it execution pauses as desired.

.vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask: Debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "args": [
                "runserver"
            ]
        }        
    ]
}

app.py

from distutils.debug import DEBUG
from flask import Flask, jsonify,request
# from flask_sqlalchemy import SQLAlchemy
import os
import click

from flask_script import Manager,Command,Server


from commands import CustomCommand

application = Flask(__name__)

manager = Manager(application)


class CustomServer(Server):
    def __call__(self, app, *args, **kwargs):
        #Hint: Here you could manipulate app
        return Server.__call__(self, app, *args, **kwargs)

# manager.add_command('db', MigrateCommand)



manager = Manager(application)
manager.add_command('runserver', CustomServer())

manager.add_command('custom_command', CustomCommand)

if __name__ == '__main__':
    manager.run()

command.py

# Custom flask script command
from flask_script import Command, Option

class CustomCommand(Command):

    def run(self):
        """Run job"""
        print("Running Command. . .")
        print("Ok")

I run the command with:

python app.py custom_command

When I place VSCode breakpoint at the print command inside the run() method of CustomCommand class, it does not pause there.

What could be the issue and the fix? Thanks in advance.

1

There are 1 best solutions below

0
On BEST ANSWER

So, the problem was in the vscode/launch.json configuration.

It seems that VSCode only looks for breakpoints when the command is executed through launch.json file. I added the configuration for custom_command and the code execution paused on the inserted breakpoint.

Here is my updated launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask: Debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "args": [
                "runserver"
            ]
        },
        {
            "name": "Flask: Custom Command",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/app.py",
            "args": [
                "custom_command"
            ]
        }        
    ]
}