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

PyInstaller打包含Plotly Express程序时Numpy依赖缺失问题

PyInstaller打包含Plotly Express的Python程序时Numpy缺失错误解决办法

问题背景

我开发了一个基于tkinter、pandas和plotly.express的程序,在PyCharm中运行完全正常,导入代码如下:

import tkinter as tk
import tkinter.filedialog as fd
import plotly.express as px
import pandas

但用PyInstaller为Windows系统打包生成EXE文件后,运行时抛出以下错误:

Traceback (most recent call last):
  File "[PATH]\main.py", line 4, in <module>
    import plotly.express as px
  File "PyInstaller\loader\pyimod02_importers.py", line 384, in exec_module
  File "plotly\express\__init__.py", line 10, in <module>
ImportError: Plotly Express requires numpy to be installed. You can install numpy using pip with:

$ pip install numpy

Or install Plotly Express and its dependencies directly with:

$ pip install "plotly[express]"

You can also use Plotly Graph Objects to create a large number of charts without installing
numpy. See examples here: https://plotly.com/python/graph-objects/

[PYI-11036:ERROR] Failed to execute script 'main' due to unhandled exception!

尝试过的无效操作:

  • 重新安装numpy和plotly-express(原本已正常安装)
  • 在main.py中直接导入numpy,触发you should not try to import numpy from its source directory错误
  • 将plotly目录添加到main.spec的datas中,打包后问题依旧
  • 在main.spec的hiddenimports中添加['numpy', 'numpy.core', 'numpy.lib.format'],打包后warn-main.txt仍提示numpy._core.XXXX类模块缺失

解决方案

1. 用干净虚拟环境重装依赖

PyInstaller打包易受全局环境混乱影响,建议创建全新虚拟环境:

# 创建并激活虚拟环境
python -m venv venv
venv\Scripts\activate

# 重装所有依赖,明确安装完整Plotly Express依赖
pip install pandas plotly[express] pyinstaller

2. 修正Spec文件配置

更新main.spec,重点优化以下几点:

  • 补充更多numpy核心子模块到hiddenimports
  • 禁用UPX压缩(部分场景下UPX会破坏numpy二进制文件)
  • 无需手动添加plotly到datas,依赖PyInstaller内置钩子处理

修改后的main.spec示例:

# -*- mode: python ; coding: utf-8 -*- 

a = Analysis(
    ['main.py'],
    pathex=[],
    binaries=[],
    datas=[],
    hiddenimports=[
        'numpy',
        'numpy.core',
        'numpy.core.multiarray',
        'numpy.core.numeric',
        'numpy.lib.format',
        'numpy.random',
        'plotly',
        'plotly.express',
        'plotly.io._html',
        'plotly.io._json'
    ],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    optimize=0,
)
pyz = PYZ(a.pure)

exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='main',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=False,  # 禁用UPX避免损坏numpy二进制
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    a.binaries,
    a.datas,
    strip=False,
    upx=False,  # 同步禁用UPX
    upx_exclude=[],
    name='main',
)

3. 提前在代码中导入numpy

在main.py最顶部预导入numpy,强制PyInstaller识别该依赖:

import numpy as np  # 提前导入numpy,避免打包时被遗漏
import tkinter as tk
import tkinter.filedialog as fd
import plotly.express as px
import pandas

注意确保是从虚拟环境的安装目录导入numpy,而非源码文件夹。

4. 更新PyInstaller到最新版

旧版本PyInstaller对numpy和plotly的兼容性较差,执行升级:

pip install --upgrade pyinstaller

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.12 06:33:24