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

问题描述 投票:0回答:4

当我尝试运行调试器时:

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 的指导

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

python visual-studio-code conda
4个回答
5
投票

您点击右下角的

Python 3.6 .....
,如下图所示

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

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


1
投票

根据您的描述,您可以尝试以下步骤来使用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环境的使用。


0
投票

在我的例子中,Visual Studio Code 版本:1.70.2,Python 环境选择位于右下角。我点击那里并将环境更改为 conda 位置。要查找您的 conda 环境和相应的 python 版本,请发出命令:pip --version 和 python --version。 Where to click in VisualStudioCode


0
投票

我的 python 选择位于右下角。它已设置为我想要的环境。

但是,调试器会在基础环境中启动。

我已将“python”:“/Users/XXX/opt/anaconda3/envs/MY-ENV/bin/python”添加到launch.json

我现在发现它在该路径上使用了python解释器,但环境仍然是基础。

© www.soinside.com 2019 - 2024. All rights reserved.