使用 Spyder 在 Anaconda 环境中导入 Numpy 错误 (WINDOWS)

Numpy Import Error in Anaconda Environment using Spyder (WINDOWS)

背景

就在两天前,我能够 运行 任何具有 numpy 依赖项的程序。现在,当我尝试使用 pandas、matplotlib 或任何依赖于 numpy 的模块 运行 我的代码时,出现以下错误:

Traceback (most recent call last):

  File "<ipython-input-8-8fcf286af663>", line 7, in <module>
    import numpy

  File "path\to\Python\Python38\site-packages\numpy\__init__.py", line 140, in <module>
    from . import core

  File "path\to\Python\Python38\site-packages\numpy\core\__init__.py", line 48, in <module>
    raise ImportError(msg)

ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

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.7 from "C:\ProgramData\Anaconda3\pythonw.exe"
  * The NumPy version is: "1.19.5"

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

Original error was: No module named 'numpy.core._multiarray_umath'

设置检查

return 消息引用了这个网站:https://numpy.org/devdocs/user/troubleshooting-importerror.html 但没有任何帮助。

我检查了我的 Path 变量以确保所有必需的目录位置都在那里并且没有问题。

当我使用 python -c "import numpy; print('done')"

从 cmd 编译代码时,我成功导入了 numpy

我正在使用 anaconda 环境,Spyder IDE,两天前它还是很好。

我检查了 pythonw.exe 的 python 版本,它看起来也不错

我的环境可能出了什么问题?

此处尝试解决方案

此外,我检查了以下链接但没有成功:

Importing the numpy c-extensions failed

importing numpy package in Spyder, Python

python Spyder not importing numpy

https://github.com/numpy/numpy/issues/15090

Can't import numpy anaconda

原来我的问题很简单。我尝试的主要解决方案是卸载并重新安装。

每次安装和卸载模块时,我都是从标准命令行执行的。

那是错误的,因为 anaconda 使用它自己的虚拟环境来存储数据。

所以我所要做的就是 运行 来自 anaconda 命令提示符的这个命令:

pip install --upgrade pandas && pip install --upgrade numpy

此命令同样有效:

pip uninstall pandas && pip uninstall numpy && pip install pandas

(因为上次安装会自动下载 pandas 具有的任何依赖项,即 numpy