You know what’s annoying? Debugging a Django app and realizing, halfway through, that you forgot to reset your database or run migrations. It’s repetitive, it’s boring, and worst of all, it’s easy to forget. Why not let your tools handle it for you? If you’re using Visual Studio Code, you can set things up so your script runs automatically every time you start the debugger.
Here’s how to do it—quick and simple.
The Problem
Let’s say you’ve got a little shell script called setup.sh
that preps your environment:
#!/usr/bin/env bash
source .venv/bin/activate
sudo su - postgres -c "dropdb docrepo"
sudo su - postgres -c "createdb docrepo -O admin"
cd docrepo
python manage.py migrate
cd ..
This resets the database and runs migrations. Great. But you’re tired of running it manually every time you debug. Why should you have to remember something a computer could do for you?
The Solution: Automate It with VSCode
The trick here is to hook your script into VSCode’s debugging workflow using tasks.json
and launch.json
. With just a little setup, you can have your script run automatically before the debugger starts.
Step 1: Define the Task
First, you need to tell VSCode how to run your script. Open (or create) .vscode/tasks.json
and add this:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Pre-Debug Script",
"type": "shell",
"command": "./setup.sh",
"presentation": {
"reveal": "always"
},
"problemMatcher": []
}
]
}
Here’s what’s going on:
-
"label"
: The name of this task. You’ll refer to it in the next step. -
"command"
: Path to your script. Make sure it’s executable (chmod +x setup.sh
). -
"presentation"
: Controls how the output is displayed. Set it to"always"
so you can see what’s happening.
Step 2: Connect the Task to the Debugger
Next, wire this task into your debugger configuration. Open .vscode/launch.json
and edit the Django debugger settings:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/docrepo/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true,
"justMyCode": false,
"autoStartBrowser": false,
"env": {
"PYTHONPATH": "${workspaceFolder}/docrepo"
},
"preLaunchTask": "Run Pre-Debug Script"
}
]
}
-
"preLaunchTask"
: This tells the debugger to run yoursetup.sh
task before it starts. -
"args"
: The arguments passed to Django’srunserver
.-
--noreload
: Disables Django’s auto-reloading feature so your script doesn’t run twice. -
--nothreading
: Disables multi-threading, which makes debugging simpler and less unpredictable.
-
Step 3: Test It
Now for the fun part. Open the Debug panel in VSCode, select the "Python Debugger: Django"
configuration, and hit F5
. Here’s what should happen:
- Your
setup.sh
script runs. You’ll see the output in the terminal. - Once the script finishes, the Django development server starts, ready for debugging.
If something doesn’t work, check the terminal output. Most issues are permissions-related (e.g., make sure your script is executable with chmod +x setup.sh
).
Why This Is Better
This setup is simple, clear, and doesn’t waste your time. Instead of juggling multiple terminals or trying to remember yet another manual step, you let your tools handle it for you. Debugging should be about solving problems in your app, not about fighting your workflow.
If you’re wondering why --noreload
and --nothreading
are here, it’s because Django’s auto-reloading and threading features can cause weird behavior when running setup tasks. Turning them off gives you a consistent, predictable environment—exactly what you want when debugging.
That’s It
No fluff, no unnecessary complexity. Now your debugger does exactly what you need, every time. Automation like this frees you up to focus on the real work. Isn’t that what good tools are supposed to do?
Top comments (0)