[Python] How to specify conda env in Python Debugger in VScode

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    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!!

    Continue reading...

Compartilhe esta Página