Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breakpoints are not hitting in VS Code while debugging Python Flask app

I'm new to Python Flask development and VS Code. I've been trying to create an app by referring the below tutorials.

https://code.visualstudio.com/docs/python/tutorial-flask

https://github.com/Microsoft/python-sample-vscode-flask-tutorial\

https://code.visualstudio.com/docs/python/debugging

Repo for reference: https://github.com/iamshivprasad/analytics

Cloned folder structure is:

- Development
  - .vscode
    - launch.json
  - analyticspyengine
    - analytics_modules
    - controllers
    - datainterfaces
    - utils
    - __init__.py
    - analyticsservices.py
    - webapp.py
    .
    .
    .

I am able to launch the program successfully with below settings in launch.json.

     {
        "name": "Python: Flask",
        "type": "python",
        "request": "launch",
        "module": "flask",
        "env": {
            "FLASK_APP": "analyticspyengine.webapp",
            "FLASK_DEBUG": true,
            "FLASK_ENV": "development"
        },
        "args": [
            "run"
        ],
        "jinja": true
    }

Content of webapp.py

#!/usr/bin/python3
import sys
from . import app   
from . import analyticsservices

Content of analyticspyengine/__init__.py

#!/usr/bin/python3
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__)))

from flask import Flask
app = Flask(__name__) 

I'm facing the following problems. Completely helpless and frustrated!!!

1) Execution is not hitting any breakpoints

2) After launching the app with the above settings, I'm not able to stop the service. i.e, Even after stopping the debugger, the app seems to be running. I can see the requests are being served through terminal output and Postman. The process seems to be alive until VS Code is closed.

Could someone shed some light?

like image 663
iamshiv Avatar asked Feb 08 '19 12:02

iamshiv


1 Answers

It looks like turning on the automatic reload (by removing the "--no-reload" flag), makes the VS Code debugger not work anymore. VS code guys say they are working on it: https://github.com/Microsoft/vscode-python/issues/4355.

So, for the moment, you should add the "no-reload" and "no-debugger" flags in the launch.json args section.

This launch.json configuration will make VS Code debugger work:

{
    "name": "Python: Flask",
    "type": "python",
    "request": "launch",
    "module": "flask",
    "env": {
        "FLASK_APP": "application.py",
        "FLASK_ENV": "development",
        "FLASK_DEBUG": "0"
    },
    "args": [
        "run",
        "--no-debugger",
        "--no-reload"
    ],
    "jinja": true
}

More info here.

like image 173
Marius Stănescu Avatar answered Oct 06 '22 21:10

Marius Stănescu