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
- Install VS Code
- Install Python (I was using 2.7, but this should work in 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
- Set-up a virtual environment for your Python execution. I chose to do this right in the ~/FlaskApp directory.
- If you’re using python3, you can probably skip to 4. You should already have virtualenv.
- Ensure you have pip (yes, another package manager). Directions for installing pip are here.
- run
pip install virtualenv
- Use virtualenv to create a virtual environment for your python code. You can replace “venv” with any folder name you feel like.
virtualenv venv
- 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
- Open ~/FlaskApp with VS Code. You should see all of your python code, git files, and a new folder called “.vscode”.
- **Optional** I recommend setting up your project settings to filter out a few files that clutter up the UI.
- To do so, press “⌘,” or equivalent to open the User Settings. This will create a new file called settings.json under the .vscode directory.
- 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, } }
- 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" }
- Restart VSCode for your settings to take effect.
- Under your “.vscode” directory, make a file called “launch.json”.
- 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" ] }] }
- under FLASK_APP, change the “app.py” to the name of your application if it’s not app.py
- Set a breakpoint in one of your Flask methods and press F5.
- 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/
- Once you see that, open a browser and navigate to the endpoint you just set a breakpoint in.
- 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