Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a virtual environment to VS Code's launch.json?

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
        },
    ]
}
like image 376
buckage Avatar asked Sep 06 '25 22:09

buckage


2 Answers

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"
    }
    
like image 86
Mike Slinn Avatar answered Sep 10 '25 04:09

Mike Slinn


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
        }
    ]
}
like image 31
Cullen D Avatar answered Sep 10 '25 04:09

Cullen D