最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

visual studio code - How to specify conda env in Python Debugger in VScode - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question asked Feb 4 at 14:54 Guilherme ParreiraGuilherme Parreira 1,04112 silver badges33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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

发布评论

评论列表(0)

  1. 暂无评论