使用pyinstaller打包pyqt5报With the default recursion limit (1000)

时间:2025-04-20 09:53:11

使用pyinstaller打包

报错内容如下:


=============================================================
A RecursionError (maximum recursion depth exceeded) occurred.
For working around please follow these instructions
=============================================================

1. In your program's .spec file add this line near the top::

     import sys ; (() * 5)

2. Build your program by running PyInstaller with the .spec file as
   argument::

     pyinstaller 

3. If this fails, you most probably hit an endless recursion in
   PyInstaller. Please try to track this down has far as possible,
   create a minimal example so we can reproduce and open an issue at
   /pyinstaller/pyinstaller/issues following the
   instructions in the issue template. Many thanks.

Explanation: Python's stack-limit is a safety-belt against endless recursion,
eating up memory. PyInstaller imports modules recursively. If the structure
how modules are imported within your program is awkward, this leads to the
nesting being too deep and hitting Python's stack-limit.

With the default recursion limit (1000), the recursion error occurs at about
115 nested imported, with limit 2000 at about 240, with limit 5000 at about
660.

如果出现类似问题说明代码没有问题,只是它默认配置出现的问题

解决办法

当支行过一次pyinstaller后此时运行过的目录下会有一个与要打包的.py文件同名的.spec文件
打开*.spec文件在文件头添加两行代码:

import sys
(2000)

当然这个2000是不定的,由自己决定改成多少,哪怕100000都可以

关于运行

在修改*.spec文件前执行的是py文件,当修改过spec文件后为了使原程序知道你有修改过什么东西当然得使用*.spec来运行。

pyinstaller -D *.spec

完成打包后生成两个文件目录build和dist

  • build为临时文件目录完成打包后可以删除。
  • dist中存放打包的结果,可执行文件和其它程序运行的关联文件都在这个目录dist下。