Conda新建环境后import numpy时出现的问题

问题由来

本人伪程序员一枚,某日心血来潮,想使用conda+VScode的方案应对日后的python编程任务,不幸的是,在新建了环境并在VScode中选择了对应的解释器后,使用import numpy总是出现如下报错

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "D:\ProgramData\Miniconda3\envs\test\python.exe"
  * The NumPy version is: "1.20.3"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed while importing _multiarray_umath: 找不到指定的模块。

注意到VScode中使用的还是默认的Powershell,而在CMD中先激活环境再使用import numpy则可以正常运行

问题解决

查阅了大量网页资料,经试验使用pip而非conda进行卸载和重装确实可以解决问题,但对于包的管理终究是不便的,报错中https://numpy.org/devdocs/user/troubleshooting-importerror.html指示我们去VSCode support寻求帮助,其中明确写道

  • Conda environments can’t be automatically activated in the VS Code Integrated Terminal if the default shell is set to PowerShell. To change the shell, see Integrated terminal - Configuration.

结合之前观察到的在CMD中可正常运行的现象,可进行如下设置
Conda新建环境后import numpy时出现的问题_第1张图片

之后选择在终端中运行python文件时可有效激活所选的虚拟环境。

说明

该报错对应的原因较为复杂,本人所遇情况较为简单,希望可以为遇到相似问题的朋友提供参考。

你可能感兴趣的:(编程相关,vscode,python,anaconda)