Jupyter Notebook中导入NumPy与matplotlib时版本兼容报错的解决方法
Hey there, that error shows up because some underlying modules (most likely matplotlib or its dependencies) were compiled for NumPy 1.x and aren't compatible with the newer NumPy 2.4.4. Here are two straightforward fixes you can run directly in your Jupyter Notebook:
Option 1: Downgrade NumPy to a 1.x Version (Quickest Solution)
This is the simplest fix since the error explicitly recommends it. Run this command in a Jupyter cell:
!pip install numpy<2 --upgrade
After executing, restart your Jupyter kernel and try importing numpy and matplotlib again.
Option 2: Upgrade Affected Modules (Keep NumPy 2.x)
If you want to stick with NumPy 2.x's new features, try upgrading matplotlib and its dependencies to versions that support NumPy 2.x. Run this command:
!pip install matplotlib --upgrade
If that doesn't resolve the issue, you can try updating all outdated packages (use this cautiously, as it might impact other projects in your environment):
# First list outdated packages (optional) !pip list --outdated # Upgrade all outdated packages !pip install --upgrade $(pip list --outdated | awk '{print $1}' | grep -v "Package")
Restart your Jupyter kernel after upgrading, then test the imports once more.
Quick Note
If you're using a virtual environment (which is highly recommended!), double-check that you're installing/upgrading packages in the correct environment.
内容的提问来源于stack exchange,提问作者pranav_b27

