繁体   English   中英

如何在 VSCode 中使用 Mamba Miniforge CLI Windows

[英]How to use Mamba Miniforge CLI in VSCode with Windows

我正在尝试调试一些必须在 Mamba 环境中运行的 Python 代码。 为了运行代码(但不是调试),我可以打开 Miniforge Prompt 命令行应用程序,激活我的环境 ( mamba activate my_env ),然后运行我的 python 文件 ( python my_file.py )。 运行此代码会产生一个错误,我想使用 Visual Studio Code 调试界面进行追溯。 我在尝试让它在 Visual Studio Code 中运行时遇到问题,因为它似乎无法运行 Miniforge Prompt 命令行。 我也在 Windows 10 上运行。

VSCode 中的默认终端选项(对于 Windows)是 Powershell 和 CMD(和 Git Bash),它们都工作正常,但是,当我为 Miniforge 添加另一个终端方法时(通过 settings.json),它似乎不是好好工作。

这是我的 settings.json 文件:

    {
        ...,

        "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",
    }

我还修改了 launch.json 以在 miniforge CLI 中运行后激活 mamba 环境。 这是我的 launch.json 文件:

    {
        // 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"],
            }
        ]
    }

此外,这里是实际激活环境的 tasks.json 文件:

    {
        // 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"
        }]
    }

当我在 VSCode 中执行任何代码(运行或调试)时,它似乎只是使用标准 CMD 终端运行,而不是在指定的 Mamba 环境中运行。 如果有人知道如何让它工作,或者在 VSCode 中调试 python 时以任何方式激活 Mamba 环境,我们将不胜感激!

我遇到了这个问题,试图将我的 VS Code 终端配置为与 miniforge 配合使用。 在稍微摆弄一下 args 语法后,我就让它正常工作了。 我使用的是 conda,而不是 mamba,但我认为这对于激活环境并不重要。

    "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"
    },

有一个 vscode 扩展https://github.com/mamba-org/vscode-micromamba

使用此扩展,可以基于 environment.yml 文件创建 mamba 环境。 在 mamba 环境旁边生成一个.env 文件。 如果您使用 python vscode 扩展,您可以将其配置为使用 .env 文件。 使用终端 window 时,环境会自动激活。

我没有运气使用 micromamba 扩展。 我正在使用 micromamba,但解决方法应该是相同的。 尝试在您的 powershell $profile 中设置此别名:

Set-Alias conda mamba

现在,当终端在 vs 代码中启动时,env 也被激活。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM