pyinstaller demo.py打包exe程序时报错
问题描述
pyinstaller demo.py时出现如下问题:
=============================================================
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 ; sys.setrecursionlimit(sys.getrecursionlimit() * 5)
2. Build your program by running PyInstaller with the .spec file as
argument::
pyinstaller myprog.spec
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
https://github.com/pyinstaller/pyinstaller/issues following the
instructions in the issue template. Many thanks.
原因分析:
python默认的递归深度是很有限的(默认是1000),因此当递归深度超过999的样子,就会引发这样的一个异常。这个错误是在 pyinstaller在打包的过程中产生的,是我的python程序导入的库带来的问题。这需要修改系统递归层数限制(recursion limit)来解决。.spec文件是在pyinstaller打包过程中分析程序产生的。打包成exe文件可以直接从.spec文件开始。因此在执行pyinstaller过程中修改系统递归层数限制,可以通过修改*.spec文件来实现。
解决方案:
打开demo.spec文件,添加下面两行。第二行代码根据自己报错时的提示填写。
import sys
sys.setrecursionlimit(sys.getrecursionlimit()*5)
然后再打包:执行“pyinstaller demo.spec”
参考资料
1、https://blog.csdn.net/weixin_42691768/article/details/84781789
2、https://blog.csdn.net/cd_yourheart/article/details/114024773