Skip to content

Debug Django with VS Code

Daniel L. Young, Ph.D edited this page Aug 18, 2020 · 7 revisions

Debug Python Django with VS Code

Summary

This document will explain how to set up a Python (specifically Django) debugging environment inside VS Code. These instructions should work on any system: Windows, Mac, or Linux. Prerequisites

  • Python 3.x – Most of our Django projects utilize 3.8.
  • Pip
  • Virtual Environment packages from Pip:
    • virtualenvwrapper (for Linux and Mac)
      • virtualenvwrapper-win (for Windows)

Install VS Code in Linux (Fedora)

https://code.visualstudio.com/docs/setup/linux

  1. Install the package key and repository:
  2. Update package cache and install package with dnf:
    • [ sudo dnf check-update ]
    • [ sudo dnf install code ]

Debug Walkthrough

  1. Create a virtual environment for the project if one doesn’t already exist.
    • To select a specific environment, use the Python: Select Interpreter command from Command Palette (⇧⌘P).
    • Python: Select Interpreter Virtual Env
  2. Install dependencies from the requirements.txt file for the project.
    • $ pip3 install -r requirements.txt
    • $ pip3 list -o lists any outdated packages
    • $ sudo pip3 install [package_name] --upgrade updates package
    • $ source ~/.bashrc If issue finding virtualenv run the command
  3. Navigate to the folder level containing manage.py file.
  4. Open this folder level with VS Code.
  5. Create a new launch.json file for debugging:
    • Select Debug > Open configurations
    • Select the Django option
  6. Add the python virtual environment path to the launch.json file. Insert a new setting for pythonPath below the line for “program”: [ "pythonPath": "/home/dyoung11/.virtualenvs/datasearch/bin/python" ]
  7. Press F5 to begin debugging.
  8. Open a browser and navigate to localhost:8000 (the port may vary).

NOTE: At this time the debugger doesn’t automatically open a browser. This functionality is likely possible, will need to research it some more.