Skip to content
Advertisement

How to configure Visual Studio Code to debug Django app in a virtualenv?

I would like to have possibility to do a debugging of Django application in Visual Studio Code. I have a virtualenv, made a change in launch.json file that look like this:

{
    "name": "Django",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "pythonPath": "${workspaceRoot}/.venv/bin/python2.7",
    "program": "${workspaceRoot}/mysite/manage.py",
    "args": [
        "runserver"
    ],
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "DjangoDebugging",
        "RedirectOutput"
    ]
},

put several break points in the code and run it. Unfortunately, the execution is not stopped on the line with break points. I tried the same without virtualenv and everything worked perfectly.

Please, point out what I am doing wrong here.

Advertisement

Answer

For me, the following 2 changes worked

  1. Add an absolute path for pythonPath
  2. Use the "--noreload" option while starting the project

Here’s the relevant part of my config

    {
        "name": "Django",
        "type": "python",
        "request": "launch",
        "stopOnEntry": true,
        "pythonPath": "/Users/xyz/Documents/dev/my_project/my_project_env/bin/python",
        "program": "${workspaceRoot}/manage.py",
        "args": [
            "runserver",
            "0.0.0.0:8080",
            "--noreload"                
        ],
        "debugOptions": [
            "WaitOnAbnormalExit",
            "WaitOnNormalExit",
            "RedirectOutput",
            "DjangoDebugging"
        ]
    },
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement