Blog

Debugging a Flask (Python) Web Service in VS Code

Today, I had to work with several technologies I haven’t used very much. Here’s a step-by-step breakdown of getting VS Code to debug a Flask project.

Setup

  1. Install VS Code
  2. Install Python (I was using 2.7, but this should work in 3+)
  3. Create/Clone your Flask app.
    For demonstration purposes, we’re going to suppose that the app is in ~/FlaskApp throughout the rest of this post.

Execution

  1. Set-up a virtual environment for your Python execution. I chose to do this right in the ~/FlaskApp directory.
    1. If you’re using python3, you can probably skip to 4. You should already have virtualenv.
    2. Ensure you have pip (yes, another package manager). Directions for installing pip are here.
    3. run
      pip install virtualenv
    4. Use virtualenv to create a virtual environment for your python code. You can replace “venv” with any folder name you feel like.
      virtualenv venv
    5. Activate your virtual environment. All of the change you make will now be inside this environment. I used this to ensure my Flask app’s dependencies were not installed across the whole system.
      source venv/bin/activate
  2. Open ~/FlaskApp with VS Code. You should see all of your python code, git files, and a new folder called “.vscode”.
  3. **Optional** I recommend setting up your project settings to filter out a few files that clutter up the UI.
    1. To do so, press “⌘,” or equivalent to open the User Settings. This will create a new file called settings.json under the .vscode directory.
    2. Use the “files.exclude” object to hide some files that are unnecessary in your project. My User Settings look like this:
      {
       "files.exclude": {
         "**/.git": true,
         "**/.svn": true,
         "**/.hg": true,
         "**/.DS_Store": true,
         "**/__pycache__": true,
         "**/*.pyc": true,
         "**/venv": true,
       }
      }
  4. If you haven’t already, press “⌘,” to open settings. Click over to “Workspace Settings”. We want to add the path to our Python Virtual Environment. Add the following to your settings:
    {
     "python.pythonPath": "~/FlaskApp/venv/bin"
    }
  5. Restart VSCode for your settings to take effect.
  6. Under your “.vscode” directory, make a file called “launch.json”.
  7. Fill launch.json with the information below:
    {
     "version": "0.2.0",
     "configurations": [{
       "name": "Flask (linux/osx)",
       "type": "python",
       "request": "launch",
       "stopOnEntry": false,
       "pythonPath": "${config.python.pythonPath}",
       "program": "${workspaceRoot}/venv/bin/flask",
       "env": {
         "FLASK_APP": "${workspaceRoot}/app.py"
       },
       "args": [
         "run",
         "--no-debugger",
         "--no-reload" 
       ],
       "debugOptions": [
         "WaitOnAbnormalExit",
         "WaitOnNormalExit",
         "RedirectOutput" 
       ]
     }]
    }
  8. under FLASK_APP, change the “app.py” to the name of your application if it’s not app.py
  9. Set a breakpoint in one of your Flask methods and press F5.
  10. your app should start. In the debug console you should see
    * "Service Flask app "<your app name>"
    
    * Running on http://127.0.0.1:5000/
  11. Once you see that, open a browser and navigate to the endpoint you just set a breakpoint in.
  12. Voila! You should hit your breakpoint. You have successfully configured Flask debugging in VS Code.

May this save you all the time I spent figuring it out.

That’s all for now. We’ll see you again soon.

– Ryan