16

I'm using VS Code's debugger to launch a Django app. I created the virtual env using python3 -m virtualenv venv and it's shown below in the place I'd like to have it. I've checked the VS Code Django and debugging tutorials but I am no closer to an answer than I was when I started.

vscode ➜ /workspaces/docker-test-proj $ ls -l
total 0
drwxr-xr-x 8 vscode vscode 256 Apr  6 14:30 helloworld


vscode ➜ /workspaces/docker-test-proj/helloworld $ ls -l
total 136
drwxr-xr-x 11 vscode vscode    352 Apr  6 13:24 app
-rw-r--r--  1 vscode vscode 131072 Apr  6 13:42 db.sqlite3
drwxr-xr-x  8 vscode vscode    256 Apr  6 13:21 helloworld
-rwxr-xr-x  1 vscode vscode    666 Apr  6 13:19 manage.py
-rw-r--r--  1 vscode vscode    175 Apr  6 14:30 requirements.txt
drwxr-xr-x  6 vscode vscode    192 Apr  6 13:20 venv

Where do I need to put the source venv/bin/activate command in my launch.json file below to have it run when starting the VSCode debugger?

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Django",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/helloworld/manage.py",
            "args": [
                "runserver",
            ],
            "django": true
        },
    ]
}

4 Answers 4

24

You do not need to add activate to launch.json. You have 3 options (I prefer the first option):

  1. Provide the complete path to python in the virtual environment. Here is one of my launch configurations. The python entry points to the python executable image in a virtual environment.

        {
          "justMyCode": false,
          "name": "Ancient Warmth / Django-Oscar",
          "type": "python",
          "request": "launch",
          "program": "${workspaceFolder}/manage.py",
          "python": "${env:oscar}/bin/python",
          "args": [
            "runserver",
            "--noreload",
            "0.0.0.0:8001",
          ],
          "django": true
        },
    

    Note that the above refers to an environment variable called oscar that points to the virtual environment. I define oscar in .bashrc like this:

    export oscar=/var/work/django/oscar
    

    It does not matter where the environment variable is defined, so long as it is defined before VSCode runs.

  2. At the bottom of the VSCode window, near the left side, you will find the name of the Python environment being used.

    a. Click on it and a list of the workspaces drops down from the top.

    b. Select the workspace that you want to modify the Python for, then you will see a list of Python interpreters. The one you want is probably not shown, so click on "I can't find the interpreter I want to select...", then click "Find...".

    c. Navigate to your virtual environment and click on python.

  3. This option is a manual way of producing the same change as option #2. Edit the file called .vscode/settings.json in your project directory and set python.pythonPath to point to the python program in your virtual environment. Here is my complete .vscode/settings.json file:

    {
      "python.linting.pylintEnabled": true,
      "python.linting.enabled": true,
      "python.pythonPath": "/var/work/django/oscar/bin/python"
    }
    
5
  • The third answer does not work when at the bottom left the python interpreter does not have a (venv) beforehand I think but refers to the system python.
    – Timo
    Commented Dec 4, 2021 at 9:42
  • 2
    That is not my experience, all that is required is that the python interpreter is present at the specified location and that it runs.
    – Mike Slinn
    Commented Dec 5, 2021 at 13:43
  • That has worked for me. However, the execution can't find a credentials.json file in the same directory as the Python execution file.
    – Augusto
    Commented Apr 7, 2023 at 13:42
  • Method 1 is not working for me in latest version of vscode. I've added export oscar=/my/virtualenv/path to my ~/.zshrc and it didn't work
    – bayman
    Commented Dec 18, 2023 at 5:10
  • Sometimes launch.json works without specifying the python attribute, but other times the vscode-debugger use the global python instead of the one inside the venv folder, so I need to specify it. Strange.
    – vit
    Commented Jun 5 at 12:20
9

Mike Slinn's answer pointed me in the right direction but if you are a Windows user of venv, this is exactly what I did.

Open the launch.json and add the following:

 "python":"C:/Users/[pathto]/[projectfolder]/.venv/Scripts/python.exe"

Full File:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "python": "C:/Users/cullen/pathto/projectfolder/.venv/Scripts/python.exe",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}
1
  • "python": "${workspaceFolder}/<venv>/Scripts/python.exe" where <venv> is your virtual environment directory.
    – mheyman
    Commented Apr 24 at 17:45
0

What works for me is adding to launch.json this one line:

"python": "${workspaceFolder}/venv/bin/python",
-2

If you're like me and you followed this documentation you'll want to setup your launch.json file with the pythonPath.

I placed my bin directly in the root of my working directory

{
    "version": "0.2.0",
    "configurations": [
        {
            ...
            "pythonPath": "${workspaceFolder}/bin/python",
            ...
        }
    ]
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.