【问题标题】:How do I activate my conda environment for VS Code python debugger and testing?如何为 VS Code python 调试器和测试激活我的 conda 环境?
【发布时间】:2020-08-14 11:07:15
【问题描述】:

当我尝试运行调试器时:

C:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara> cmd /C "C:/Users/Philip/miniconda3/envs/capra/python.exe c:\Users\Philip\.vscode\extensions\ms-python.python-2020.8.101144\pythonFiles\lib\python\debugpy\launcher 53607 -- c:\Users\Philip\OneDrive\Betting\Capra\Tennis\polgara\updater.py "
C:\Users\Philip\miniconda3\envs\capra\lib\site-packages\numpy\__init__.py:138: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
  from . import _distributor_init

我发现如果我将numpy 安装到我的base 环境中,那么我可以绕过该错误。问题是如何为调试器激活capra环境?

我已尝试遵循 VS Code 中的 guidance

1. pythonPath property of the selected debug configuration in launch.json

我的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",
            "pythonPath": "C:/Users/Philip/miniconda3/envs/capra/python.exe"
        }
    ]
}

下一步:

2. python.pythonPath setting in the workspace settings.json

我重新启动VS Code 并得到:

We removed the "python.pythonPath" setting from your settings.json file as the setting is no longer used by the Python extension. You can get the path of your selected interpreter in the Python output channel. [Learn more](https://aka.ms/AA7jfor).

最后:

3. python.pythonPath setting in the user settings.json

我的用户设置文件:

{
    "terminal.integrated.shell.windows": "C:/WINDOWS/System32/cmd.exe",
    "kite.showWelcomeNotificationOnStartup": false,
    "python.venvPath": "C:/Users/Philip/miniconda3/envs/capra",
    "python.pythonPath": "C:/Users/Philip/miniconda3/envs/capra/python.exe",
    "terminal.integrated.automationShell.windows": "C:/WINDOWS/System32/cmd.exe",
    "python.autoComplete.extraPaths": [
    
    ]
}

仍然得到相同的 numpy 错误。

值得一提的是,在尝试发现测试后查看Python Test Log 输出时,我得到了完全相同的错误。

【问题讨论】:

  • 您是否尝试在终端中使用“conda activate capra”?当我们创建并选择一个coda环境时,VSCode会先进入base环境,用这句话激活后,就会进入新创建的'capra'环境。 (也可以使用“'ctrl+shift+`”打开一个新的终端,它会自动激活。')参考:conda.io/projects/conda/en/latest/user-guide/tasks/…
  • 是的。运行调试器似乎可以重置它。请参阅我收到的消息的第一个代码块。看起来终端不在任何环境中。但是,正如我提到的,如果我在基本环境中安装 numpy,那么错误就会消失......

标签: python visual-studio-code conda


【解决方案1】:

你点击右下角的Python 3.6 .....如下图

然后你只需要选择你已经创建的环境,如下图所示

然后你运行代码,代码将在选定的环境中运行。

【讨论】:

  • 我选择了capra 环境。使用调试器似乎让我摆脱了它并进入base
  • 只有在使用 VS Code 终端并使用命令 conda activate capra 时,调试器才会将您踢入基地......使用我提到的上述方法,它永远不会将您踢出环境。我已经确认了,我也遇到了同样的问题,但是经过多次搜索后,我展示的上述方法工作正常
【解决方案2】:

根据您的描述,您可以尝试以下步骤来使用 conda 环境: (示例:使用python3.8创建一个名为'capra'的conda环境。)

  1. 创建一个 conda 环境。

conda create -n capra python=3.8

创建完成后,重新加载VSCode,刷新加载几次。

  1. 选择 conda 环境。

    点击VSCode左下角,选择刚刚创建的环境。

  2. 激活 conda 环境。可以直接使用快捷键:Ctrl+Shift+`

调试代码时,终端显示在创建的环境中:

  1. 在当前的 conda 环境中安装所需的模块。

另外,对于你提到的'removed "python.pythonPath" setting',原因是你设置了'workspace settings.json'。现在python扩展在这里没有使用'pythonPath'。当我们在两个设置文件中同时设置时,它将被删除。这不会影响conda环境的使用。

【讨论】:

  • 为什么我需要创建一个新环境?根据我希望确保调试器使用capra 环境运行的问题?
  • @Jossy 是的,您可以使用您创建的 capra 环境,然后激活它。您可以从第二步开始引用它。提供完整的步骤供您参考。
  • @Jossy 为了更好地解释这个过程,我编辑了上面的内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
相关资源
最近更新 更多