当你运行导入pandas 库的Python脚本时,你可能会遇到一个错误,说缺少numpy。
错误输出的例子如下:
Traceback (most recent call last):
File "/Users/nsebhastian/Desktop/Sandbox/python/test.py", line 1,
in <module>
---> import pandas as pd
File "/opt/homebrew/lib/python3.9/site-packages/pandas/__init__.py",
in <module>
17 if missing_dependencies:
18 raise ImportError(
---> 19 "Missing required dependencies {0}".format(missing_dependencies))
20 del hard_dependencies, dependency, missing_dependencies
21
ImportError: Missing required dependencies ['numpy']
当在你的 Python 环境中找不到numpy 包时,就会发生上面的错误。
为了解决这个错误,你需要确保numpy 包已经安装在你的 Python 环境中。
注意,numpy 包必须安装在你运行脚本的环境中。
从终端安装numpy
当你从终端运行你的Python脚本时,那么你需要看看numpy 库是否已经安装在你的计算机上。
你可以通过使用pip show [library_name] 命令来做这件事。
下面是一个关于pandas 和numpy 库的输出示例:
pip show pandas
Name: pandas
Version: 1.4.2
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author: The Pandas Development Team
Author-email: pandas-dev@python.org
License: BSD-3-Clause
Location: /opt/homebrew/lib/python3.9/site-packages
Requires: numpy, python-dateutil, pytz
Required-by:
pip show numpy
Name: numpy
Version: 1.22.3
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License: BSD
Location: /opt/homebrew/lib/python3.9/site-packages
Requires:
Required-by: pandas
如果你看到下面的输出:
pip show numpy
WARNING: Package(s) not found: numpy
那么这意味着你需要用以下命令安装numpy :
pip install numpy
一旦安装了numpy ,你应该能够使用终端运行Python脚本。
使用Anaconda和Conda安装numpy
如果你使用的是Anaconda发行版,那么你需要确保在Conda环境中安装numpy 包。
你可以通过在终端运行conda list 命令来检查conda上安装的软件包列表:
conda list | grep numpy
numpy 1.21.2 py39h4b4dc7a_0
numpy-base 1.21.2 py39he0bd621_0
numpydoc 1.1.0 pyhd3eb1b0_1
如果你没有看到类似于上图的输出,那么你需要用下面的命令安装numpy :
conda install numpy
终端将为你安装numpy 包。
另外,你可以打开Anaconda导航器,选择Environments菜单来查看你环境中安装的软件包。
在下面的截图中,numpy 库被发现在Condabase 环境中:

发现Anaconda Navigator numpy库
(anaconda-nav-numpy.png)
如果你没有看到已安装的numpy 库,那么你需要点击复选框并应用更改。
一旦numpy ,重新启动你的Jupyter Notebook或Spyder程序。现在错误应该已经解决了。
将pandas和numpy都升级到最新版本
有时,你也可能因为你安装的pandas 和numpy 库的旧版本而看到这个错误。
你可以尝试将这两个库升级到最新版本,看看错误是否会消失。
下面是pip 的命令:
pip install --upgrade pandas
pip install --upgrade numpy
如果你使用的是Conda:
conda install pandas
conda install numpy
这就是你如何解决你的Python项目中的missing required dependencies ['numpy'] 错误。
我希望这个教程对你有帮助。🙏