How to use Mamba Miniforge CLI in VSCode with Windows
Asked Answered
H

4

9

I am trying to debug some Python code that must run in a Mamba environment. In order to run the code (but not debug), I can open the Miniforge Prompt command line application, activate my enviroment (mamba activate my_env), and then run my python file (python my_file.py). Running this code is producing an error that I would like to trace back using the Visual Studio Code debugging interface. I am having a problem trying to get this to run in Visual Studio Code, because it cannot seem to run the Miniforge Prompt command line. I am also running on Windows 10.

The default terminal options (for Windows) in VSCode are Powershell and CMD (and Git Bash), which both work fine, however, when I added another terminal method for Miniforge (via the settings.json), it doesn't seem to be working properly.

Here is my settings.json file:

    {
        ...,

        "terminal.integrated.profiles.windows": {

            "PowerShell": {
                "source": "PowerShell",
                "icon": "terminal-powershell"
            },
            "Command Prompt": {
                "path": [
                    "${env:windir}\\Sysnative\\cmd.exe",
                    "${env:windir}\\System32\\cmd.exe"
                ],
                "args": [],
                "icon": "terminal-cmd"
            },
            "Git Bash": {
                "source": "Git Bash"
            },
            "MambaPython": {
                "path": [
                    "${env:windir}\\System32\\cmd.exe"
                ],
                "args": ["\"/K\"", "C:\\ProgramData\\mambaforge\\Scripts\\activate.bat", "C:\\ProgramData\\mambaforge"],
                "icon": "terminal-cmd"
            }
        },
        "terminal.integrated.defaultProfile.windows": "MambaPython",
    }

I also modified the launch.json to activate the mamba environment once running in the miniforge CLI. Here is my launch.json file:

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Current File",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "justMyCode": true,
            },

            {
                "name": "Python: ProjectEnv",
                "type": "python",
                "request": "launch",
                "program": "${file}",
                "console": "integratedTerminal",
                "justMyCode": true,
                "preLaunchTask": "ProjectEnv_activate",
                "args": ["--kwarg_one=Something", "--kwarg_two"],
            }
        ]
    }

also, here is the tasks.json file that actually activates the environment:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [{
            "label": "ProjectEnv_activate",
            "command": "mamba activate ProjectEnv",
            "type": "shell"
        }]
    }

When I execute any code (in run or in debug) in VSCode, it appears to just run with the standard CMD terminal, not in a Mamba environment as specified. If anyone knows how to get this to work, or any way to activate a Mamba environment when debugging python in VSCode, any help would be much appreciated!

Housing answered 2/5, 2022 at 21:45 Comment(0)
S
2

It seems like the VS Code Micromamba extension would work for this. It will allow you to activate existing Mamba environments or create new ones from within VS Code: https://github.com/mamba-org/vscode-micromamba

Sprinkle answered 10/6, 2023 at 19:33 Comment(0)
C
1

I came across this question trying to configure my VS Code terminal to play nice with miniforge. I got it working correctly after just fiddling with the args syntax a bit. I'm using conda, not mamba, but I don't think that should matter for activating the environment.

    "Command Prompt": {
        "path": [
            "${env:windir}\\System32\\cmd.exe"
        ],
        "args": ["/K", "C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\Scripts\\activate.bat","C:\\Users\\johndoe\\AppData\\Local\\miniforge3\\envs\\base" ],
        "icon": "terminal-cmd"
    },
Cool answered 6/6, 2022 at 22:37 Comment(2)
If you just run code in miniforge, it will open the VS CodeUndry
"Command Prompt" isn't anything VS Code (Version 1.87.2) is recognizing in workspace or user settings json. Couldn't find any documentation for this to look for an alternative unfortunately.Forefather
A
0

I had no luck with the micromamba extension. I'm using micromamba, but the workaround should be the same. Try set this alias in your powershell $profile:

Set-Alias conda mamba

Now the env is also activated, when the terminal starts in vs code.

Alphitomancy answered 14/12, 2022 at 7:19 Comment(2)
Can I ask what is not working with micromamba extension? It's not only activating environment in terminal but also for other extensions - e.g. pythonHannan
To set the python interpreter i have to browse to the python.exe in my micromamba folder. That works. But the extension doesn't really help me at this point.Alphitomancy
M
0

I opened miniforge terminal to find default path:

where python

Then in vscode settings:

python settings

Mcgruter answered 5/9, 2023 at 20:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.