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

使用cx_Freeze打包含Pandas/Matplotlib的Python脚本遇递归错误求助

解决cx_Freeze打包含Pandas、Matplotlib脚本时的递归深度错误

方法1:调高递归深度限制

cx_Freeze处理复杂依赖库时容易触发Python默认的递归上限,可在setup.py开头手动调高限制:

import sys
sys.setrecursionlimit(5000)  # 可根据实际情况调整为5000或更高值

from cx_Freeze import setup, Executable

setup(name="Test file", version="1.0", 
      options = {'build_exe': {'packages': ['pandas', 'os', 'subprocess', 'matplotlib', 'glob', 'datetime']}}, 
      description="Just a test file", 
      executables=[Executable("Test_file.py")])

注意:无需单独列出matplotlib.pyplot、matplotlib.tkinter,仅需指定matplotlib,cx_Freeze会自动处理其子模块。

方法2:排除冗余依赖模块

部分numpy相关的测试、工具模块会引发不必要的递归,可在打包配置中手动排除:

import sys
sys.setrecursionlimit(5000)

from cx_Freeze import setup, Executable

setup(name="Test file", version="1.0", 
      options = {'build_exe': {
          'packages': ['pandas', 'os', 'subprocess', 'matplotlib', 'glob', 'datetime'],
          'excludes': ['numpy.testing', 'numpy.distutils']
      }}, 
      description="Just a test file", 
      executables=[Executable("Test_file.py")])

方法3:明确指定Matplotlib后端

在你的业务脚本Test_file.py开头添加后端指定代码,避免打包时的依赖解析混乱:

import matplotlib
matplotlib.use('TkAgg')  # 明确指定tkinter后端
import matplotlib.pyplot as plt
import matplotlib.tkinter

方法4:升级相关库版本

递归错误可能是旧版本cx_Freeze的已知bug,升级到最新稳定版可解决问题:

pip install --upgrade cx_Freeze pandas matplotlib

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 10:04:56