How to use Mamba Miniforge CLI in VSCode with Windows

6.4k views Asked by At

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!

4

There are 4 answers

2
nlaan On

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.

2
Jelly On

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"
    },
0
Tom F On

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

0
mmann1123 On

I opened miniforge terminal to find default path:

where python

Then in vscode settings:

python settings