You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyInstaller打包Python代码为EXE时出现numpy模块缺失错误

解决Anaconda环境下PyInstaller打包缺失numpy的问题

我之前在Anaconda环境里用PyInstaller打包带numpy的脚本时,也碰到过一模一样的「运行exe提示ModuleNotFoundError: No module named 'numpy'」的问题,给你几个亲测有效的排查和解决方向:

  • 确保PyInstaller属于当前Anaconda环境
    很多时候问题出在你用了全局Python环境安装的PyInstaller,它会去全局环境找依赖,而不是Anaconda的虚拟环境。你可以在激活Anaconda环境的Prompt里输入:

    where pyinstaller
    

    查看输出的路径是否在你的Anaconda环境目录下(比如C:\Users\XXX\Anaconda3\envs\你的环境名\Scripts\pyinstaller.exe)。如果不是,先卸载全局的PyInstaller:

    pip uninstall pyinstaller
    

    然后在激活的Anaconda环境里重新安装对应版本:

    conda install pyinstaller=3.5
    # 或者用pip安装
    pip install pyinstaller==3.5
    
  • 手动指定numpy的路径给PyInstaller
    有时候PyInstaller没法自动识别Anaconda环境里numpy的位置,你可以先找到numpy的安装路径:在Python交互环境里运行:

    import numpy
    print(numpy.__path__)
    

    得到路径后,打包时用--paths参数指定,比如:

    pyinstaller --hidden-import=numpy --paths "C:\Users\XXX\Anaconda3\envs\你的环境\Lib\site-packages\numpy" your_script.py
    
  • 修改spec文件添加更细致的hidden imports
    PyInstaller3.5对numpy的自动依赖收集可能不够全面,你可以先生成spec文件:

    pyinstaller your_script.py
    

    然后打开生成的your_script.spec文件,找到hiddenimports这一行,把numpy的核心子模块也加进去:

    hiddenimports=['numpy', 'numpy.core', 'numpy.core.multiarray', 'numpy.core.umath'],
    

    保存后用spec文件重新打包:

    pyinstaller your_script.spec
    
  • 清理PyInstaller缓存后重新打包
    旧的build缓存可能会导致依赖收集异常,每次打包前记得删除build、dist文件夹,或者直接用--clean参数强制清理:

    pyinstaller --clean --hidden-import=numpy your_script.py
    
  • 检查numpy版本兼容性
    Python3.6不再支持numpy1.20及以上版本,如果你安装的numpy版本过高,可能会和PyInstaller3.5不兼容。你可以降级到兼容的版本试试:

    conda install numpy=1.19.5
    # 或者用pip
    pip install numpy==1.19.5
    

    安装完成后重新打包测试。

内容的提问来源于stack exchange,提问作者Altaris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:52:37