问题

你在运行python代码的时候,是否遇到过下面这种错误

ImportError: Unable to import required dependencies:numpy: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!Importing the numpy C-extensions failed. This error can happen formany reasons, often due to issues with your setup or how NumPy wasinstalled.We have compiled some common reasons and troubleshooting tips at:https://numpy.org/devdocs/user/troubleshooting-importerror.htmlPlease note and check the following:* The Python version is: Python3.7 from "C:\Users\MSI-NB\.conda\envs\python37\python.exe"* The NumPy version is: "1.21.6"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: 找不到指定的模块。

如上信息所示,我的python版本是3.7,Numpy版本是1.21.6,他们的版本不匹配,导致运行报错。

解决方法

1.通过安装对应的版本号,python3.7对应的是numpy1.19.5,直接按照对应版本即可。
如果你使用的pip,只需要执行

pip uninstall numpypip install numpy==1.19.5

如果你使用的是conda,需要执行

conda uninstall numpyconda install numpy==1.19.5

那么如何知道他们的对应关系呢,这个网站你一定要收藏
https://www.lfd.uci.edu/~gohlke/pythonlibs/

在这里,你能够看到所有python库和python版本的对应关系。以pandas为例,如下图所示,cp后面指的就是python版本,cp39就是python3.9,那么它对应的pandas版本就是pandas==1.3.5

如果依然无法解决你的问题,可以尝试安装numpy-base

conda install -c anaconda numpy-base

如果依旧没有解决,你可以看下你的pip下和conda下是不是都有Numpy,通过执行pip list 和conda list,如果是的话,需要将pip种的删除,pip uninstall numpy.

不过的执行后,又报了另外一个错误,

~\.conda\envs\python37\lib\site-packages\pandas\_typing.py in  82 # array-like 83 ---> 84 ArrayLike = Union["ExtensionArray", np.ndarray] 85 AnyArrayLike = Union[ArrayLike, "Index", "Series"] 86 AttributeError: module 'numpy' has no attribute 'ndarray'

这个报错是因为numpy和panda版本不匹配导致的,需要卸载后重新安装,注意,需要先安装numpy,再安装pandas