VSCode在终端启动时不使用conda环境

5
我正在使用Visual Studio Code (VSCode)在Mac上进行Python编程。我使用conda来管理Python环境。当我从VSCode中打开一个文件夹(使用菜单中的File -> Open...)时,它会打开选定的Python文件夹。我选择一个Python文件进行编辑,而VSCode选择了我的默认conda环境,名称为base。当我打开集成的VSCode终端时,它也会使用base conda环境。下面是这个过程的屏幕截图。

screen capture working example

我还可以使用code命令从macOS终端启动VSCode。从Mac终端中,我使用cd进入包含Python文件的文件夹。在该目录中,我使用code .命令打开VSCode。我选择一个Python文件进行编辑,而VSCode选择了我的默认conda环境,名称为base。当我打开集成的VSCode终端时,它显示我正在使用base conda环境。然而,集成的VSCode终端并没有使用conda环境的Python和包。下面是这一点的屏幕截图。

screen capture of nonworking example

我的VSCode设置如下:
{
    "telemetry.telemetryLevel": "off",
    "python.condaPath": "/opt/miniconda3/bin/conda",
    "python.defaultInterpreterPath": "/opt/miniconda3/bin/python",
    "python.pythonPath": "/opt/miniconda3/bin/python",
    "python.terminal.activateEnvInCurrentTerminal": true,
    "python.terminal.activateEnvironment": true,
    "python.linting.flake8Args": [
        "--max-line-length=120"
    ],
    "breadcrumbs.enabled": false,
    "terminal.integrated.defaultProfile.osx": "zsh",
    "terminal.explorerKind": "external",
    "terminal.integrated.inheritEnv": false,
    "python.linting.flake8Enabled": true,
    "editor.fontSize": 13,
    "terminal.integrated.fontSize": 13,
    "workbench.colorTheme": "Dracula Soft",
}

如何让集成的VSCode终端在从macOS终端启动应用程序时使用默认的conda环境?

你是重新打开已选择基本conda环境的文件夹还是打开一个新文件夹? - Steven-MSFT
1
@merv 关于VSCode的conda相关问题有很多,但我还没有看到关于这个特定问题的任何问题。 - wigging
@wigging 看起来很奇怪。在 code . 路径中,如果你创建一个新的终端会发生什么?如果你手动激活 anaconda 会发生什么? - Steven-MSFT
如果您切换到其他Python解释器,它是否可以工作?然后再切换回conda base?这真的很奇怪。 - Steven-MSFT
@Steven-MSFT 不行,那样不起作用。无论我怎么做,当使用 code . 启动 VSCode 应用程序时,集成的 VSCode 终端都会使用系统 Python。 - wigging
显示剩余9条评论
2个回答

2
今日免费次数已满, 请开通会员/明日再来
"python.condaPath": "C:\\Users\\xxxx\\mambaforge\\Scripts\\conda.exe"

(其他Windows用户请注意:我还将默认终端切换为命令提示符,因为conda和Powershell之间并不完美兼容)
我看到您在设置文件中有这个变量,但是您只是引导了驼鸟去喝水(/opt/miniconda3/bin/conda),而没有把它的脸塞进桶里(我假设是/opt/miniconda3/bin/conda/Scripts/conda.exe),所以您可以尝试一下。我知道您目前使用的方法直到我指向了Scripts文件夹中的conda.exe(其中激活bat位于其中)才解决了我的问题。
另一个需要考虑的问题:我没有在我的settings.json中设置任何这些行(有一个例外,已注明)。
{
    "python.defaultInterpreterPath": "/opt/miniconda3/bin/python",
    "python.pythonPath": "/opt/miniconda3/bin/python",
    "python.terminal.activateEnvInCurrentTerminal": true,
    "python.terminal.activateEnvironment": true,
    "python.linting.flake8Args": [
        "--max-line-length=120"
    ],
    "breadcrumbs.enabled": false,
    "terminal.explorerKind": "external",
    "terminal.integrated.inheritEnv": false, // <-- I actually have this set to 'true'
    "python.linting.flake8Enabled": true,
}

并不是说这些都一定会在这里起作用,我只是想提出来以防有所帮助。


实际上我遇到了一个噩梦般的问题,完全与terminal.integrated.inheritEnv被设置为FALSE有关(这是我过去设定的);当我的例程在其他地方都能正常工作时,pytest却失败了。将其设置回true立即解决了问题。bashrc被同样引用,所有环境变量都相同,Python实例也相同,但这毫无意义。请将其保持为TRUE! - Carl Boneri

1
在VS Code的命令调色板中,使用“> Python:Select Interpreter”,并选择您想要使用的Python版本。一旦选择完成,它就应该被设置为默认版本,因此这就是您所需的。为了在终端中激活环境,请确保VS Code中的“python.terminal.activateEnvironment”设置为true。
来源:Visual Studio Code Documentation

我已经将我的VSCode设置添加到问题中。我已经启用了python.terminal.activateEnvironment - wigging

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接