Keath Milligan

On software development, quality, security and broken racecars

Debugging a Flask 0.11 app in Visual Studio Code

Visual Studio Code with the the Python extension makes for a great Python development environment – especially if you work on blended Python/Javascript web apps. The Python extension includes a built-in template launch definition for debugging a Flask app, but it assumes you have an old-style “run.py” script in your project. If you are using Flask 0.11.x, chances are you use the new “flask” command to launch your app.

First, be sure you have your virtual environment configured in VSCode (you are using a virtual environment, right?). Select Preferences > Workspace Settings from the menu. Your .vscode/settings.json file should have a line something like this:

In the .vscode directory, create a file named launch.json if it does not already exist. Change the “Flask” entry as follows:

Linux/Unix/macOS:

Windows:

Also on Windows, you will need to add a “run.py” launcher script to the root of your project. This script will use the Flask CLI module to launch your app so it will behave consistently with “flask run”:

run.py:

Notes:

  • Unless you really need to debug app startup, set stopOnEntry to false so it won’t break in library code.
  • Change program to the path to your “flask” command in the virtual environment. You can get this from the command line (with the virtual environment active) by typing “which flask”.
  • Change the FLASK_APP environment variable to the name of your app’s bootstrap file.
  • Set --no-debugger to avoid any potential conflicts with the Werkzueg debugger.
  • Set --no-reload. The Python debugger doesn’t support module reloading.

Now set some breakpoints and start debugging!

 

5 Comments

  1. Thanks for your post, I have updated the wiki with your instructions and will ensure to add that as a default default configuration for flask.

  2. It doesn’t work on windows

Leave a Reply

Your email address will not be published.

*

© 2017 Keath Milligan

Theme by Anders NorenUp ↑