Search code examples
pythonvisual-studio-codeanacondacondavscode-debugger

How to specify conda env in Python Debugger in VScode


Problem

When I want to debug a Python file and hit the button Python Debugger: Debug Python File or Debugin with JSON, it selects the default conda environment.

Workaround

To fix that, I manually open the terminal (created by the debugger), conda activate conda_name, and then ask to debug again, and it works. However, it is tedious.

Desired

So, I tried to automate the conda environment activation with launch.json, and tasks.json through "preLaunchTask". but it does not select the conda env specified. My attempt:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal", // internalConsole -> to prevent from printing
            "cwd": "${fileDirname}", // "${workspaceFolder}/src", "${workspaceFolder}",
            "purpose": [
                "debug-in-terminal"
            ],
            "justMyCode": false,
            "subProcess": true,
            "logToFile": true,
            "pythonArgs": ["-Xfrozen_modules=off"],



            "env": {
                "CONDA_DEFAULT_ENV": "/mnt/data/mochinski/.conda/envs/sklearn",
            },
            "python": "/mnt/data/mochinski/.conda/envs/sklearn/bin/python",
            "preLaunchTask": "activate-conda-env",
        }
    ]
}

tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "activate-conda-env",
            "type": "shell",
            "command": "conda init && conda activate /mnt/data/mochinski/.conda/envs/sklearn",
            "problemMatcher": []
        }
    ]
}

Thanks!!


Solution

  • I managed to solve it by specifying correctly the env:

    "env": {
         "PATH": "/mnt/data/gui/gpu_time_series/bin:${env:PATH}",
         "CONDA_DEFAULT_ENV": "/mnt/data/gui/gpu_time_series",
    },
    

    So, my path will concatenate to the PATH of the system. And I don't need the ar

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python Debugger: Current File",
                "type": "debugpy",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal", // internalConsole -> to prevent from printing
                "cwd": "${fileDirname}", // "${workspaceFolder}/src", "${workspaceFolder}",
                "purpose": [
                    "debug-in-terminal"
                ],
                "justMyCode": false,
                "pythonArgs": ["-Xfrozen_modules=off"],
                "env": {
                    "PATH": "/mnt/data/gui/gpu_time_series/bin:${env:PATH}",
                    "CONDA_DEFAULT_ENV": "/mnt/data/gui/gpu_time_series",
                },
                "args": [
                    "-path_yml",
                    "../data/01_raw/fixed/local/config_317_anp_fast_id_21_170.yml",
                ],
            }
        ]
    }
    

    And, yes, I had to change the conda environment. However, the logic should work for the environment in the question.

    I am using Python Debugger 2025.0.0