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

使用PyInstaller生成的EXE运行时出现ImportError问题求助

PyInstaller打包含NumPy脚本后本地运行DLL加载失败的解决办法

问题描述

Python代码使用NumPy模块,直接运行脚本正常,但通过PyInstaller打包为EXE后,本地运行报错(该EXE在另一台电脑可正常运行),错误信息如下:

Traceback (most recent call last):
  File "numpy\core\__init__.py", line 24, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "numpy\core\multiarray.py", line 10, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "numpy\core\overrides.py", line 8, in <module>
ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "numpy\__init__.py", line 159, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "numpy\__config__.py", line 4, in <module>
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "numpy\core\__init__.py", line 50, in <module>
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "c:\test_numpy.exe"
  * The NumPy version is: "1.26.4"

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 while importing _multiarray_umath: The specified module could not be found.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\test_numpy.py", line 1, in <module>
    import numpy as np
  File "PyInstaller\loader\pyimod03_importers.py", line 540, in exec_module
  File "numpy\__init__.py", line 164, in <module>
ImportError: Error importing numpy: you should not try to import numpy from
        its source directory; please exit the numpy source tree, and relaunch
        your python interpreter from there.
[58772] Failed to execute script test_numpy

解决方法

1. 适配NumPy与Python版本

卸载当前NumPy,安装和Python3.9兼容的稳定版本:

pip uninstall numpy -y
pip install numpy==1.23.5

同时更新PyInstaller到最新稳定版:

pip install --upgrade pyinstaller

2. 强制收集NumPy所有依赖

打包时添加--collect-all numpy参数,确保PyInstaller打包NumPy的全部关联文件:

pyinstaller --collect-all numpy test_numpy.py

3. 安装缺失的系统依赖

错误根源可能是本地缺少NumPy依赖的Visual C运行库,安装对应Python3.9的Microsoft Visual C Redistributable(2015-2022版本)。

4. 清理缓存后重新打包

删除PyInstaller生成的build、dist文件夹及.spec文件,再重新执行打包命令,避免缓存干扰。

5. 使用干净虚拟环境打包

  • 创建虚拟环境:
    python -m venv venv
    
  • 激活虚拟环境(Windows):
    venv\Scripts\activate
    
  • 安装依赖并打包:
    pip install numpy pyinstaller
    pyinstaller test_numpy.py
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 22:08:19