必须先升级pip和setuptools至兼容版本,再安装numpy==2.0.1:python -m pip install --upgrade pip setuptools wheel;若仍报错则强制回退setuptools==67.0.0;最后pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy==2.0.1。

pip install numpy 在 Python 3.12 下必报 AttributeError: module 'pkgutil' has no attribute 'ImpImporter'
这不是 NumPy 本身的问题,而是构建链断裂:Python 3.12 彻底移除了 pkgutil.ImpImporter,但旧版 pip 和 <code>setuptools 仍在硬编码调用它。只要 pip 或 setuptools 版本不达标,哪怕你装的是 <code>numpy==2.0.1,安装过程也会在构建阶段直接崩溃。
常见错误堆栈开头就是:
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'
此时重试、换源、加 --user 都无效——底层工具不兼容,属于硬性阻断。
- 必须用
python -m pip install --upgrade pip setuptools wheel升级当前解释器绑定的 pip(不能只运行pip install --upgrade) - 若升级后仍报错,说明 setuptools 没真正更新到位,强制回退:
pip install --force-reinstall setuptools==67.0.0 - 验证:
pip --version≥ 23.3,且python -c "import setuptools; print(setuptools.__version__)"输出 ≥ 67.0.0
PyPI 上只有 numpy==2.0.1 完整支持 Python 3.12 预编译 wheel
截至 2026 年 9 月,PyPI 上为 Python 3.12 提供全平台预编译 .whl 的 NumPy 版本仅限 numpy==2.0.1。后续版本如 2.1.0、2.1.1 虽已发布,但尚未覆盖所有架构(尤其是 macosx_12_0_arm64、manylinux_2_17_x86_64 等),直接 pip install numpy 会 fallback 到源码编译,而编译又依赖系统 C 工具链和 BLAS 库,极易失败。
立即学习“Python免费学习笔记(深入)”;
让 macOS 自动化像说话一样简单 —— 用代码控制你的 Mac,无需学习 AppleScript 系统要求 macOS 14+ (Sonoma) Python 3.9+ 需要开启:辅助功能 + 屏幕录制权限
- 务必显式指定版本:
pip install numpy==2.0.1 - 国内用户加清华源加速:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy==2.0.1 - macOS ARM64 / Linux aarch64 用户若提示
no matching distribution,需手动下载对应.whl文件后本地安装:pip install numpy-2.0.1-cp312-cp312-macosx_12_0_arm64.whl
conda install numpy 是更稳妥的替代方案
如果你在 Linux/macOS 上反复遭遇 Failed building wheel for numpy,或 Windows 提示 Microsoft Visual C++ 14.0 is required,说明 pip 编译路径已不可靠。conda 不走 PyPI,而是从 anaconda.org 或镜像拉取预打包的 .tar.bz2 二进制包,完全绕过本地编译。
- 先清理 pip 残留:
pip uninstall numpy - 配置清华 conda 镜像(国内必需):
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/(重复三次,分别加pkgs/free和cloud/conda-forge) - 然后直接:
conda install numpy—— 自动选匹配 Python 3.12 的版本,且一并解决openblas、libgfortran等底层依赖 - PyCharm/VSCode 中必须手动切换解释器路径到 conda env 的
python,否则 IDE 仍会读取旧 pip 环境
验证是否真成功,而非“假安装”
很多用户看到 Successfully installed numpy-2.0.1 就以为完事,结果运行时仍报 ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'。这是因为:
- 安装路径和运行解释器不一致(比如用系统 pip 装,却在 conda env 里跑代码)
- 缓存了旧版
numpy-1.x的.pyc或构建中间文件 - 装了错架构的 wheel(如在 Apple Silicon Mac 上装了
cp312-cp312-macosx_10_9_x86_64.whl)
用干净命令行验证:
python3.12 -c "import numpy as np; print(np.__version__, np.__file__)"
np.__file__ 必须指向 site-packages/numpy-2.0.1.dist-info/ 下的真实路径,且不能含 build/、temp/ 或 numpy-1. 字样;若报 dlopen(): incompatible architecture,立刻换对应平台的 wheel。

















