如何激活VS Code python调试器和测试的conda环境?

时间:2020-08-14 11:07:15

标签: python visual-studio-code conda

当我尝试运行调试器时:

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输出时,我得到了完全相同的错误。

2 个答案:

答案 0 :(得分:1)

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

  1. 创建一个conda环境。

conda create -n capra python=3.8

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

  1. 选择conda环境。

    单击VSCode的左下角以选择刚刚创建的环境。 enter image description here

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

    enter image description here

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

enter image description here

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

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

答案 1 :(得分:1)

您点击Python 3.6 .....右下角,如下图Click on red square part Python 3.6. ....

然后,您只需要选择已经创建的环境,如下图所示 Select the environment in red circle

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