使用PyInstaller打包Python程序遭遇RecursionError问题求助
Hey there, let's work through this recursion error you're facing. I've seen similar issues with older PyInstaller versions and openpyxl's nested module structure—here are actionable steps to try:
1. Upgrade PyInstaller (Critical First Step)
PyInstaller 3.3.1 is pretty outdated (released back in 2018), and it had known recursion-related bugs when handling certain library structures like openpyxl's nested modules. For Python 3.6.4, the latest compatible PyInstaller version is 3.6 (since later versions dropped support for Python 3.6). Install it with:
pip install pyinstaller==3.6
This alone fixes most recursion issues with openpyxl and pandas.
2. Tweak Recursion Limits Properly
If upgrading doesn't immediately solve it, simply setting sys.setrecursionlimit() in your script won't help—PyInstaller's analysis phase has its own recursion limit. Instead:
- Generate a spec file first:
pyinstaller --name your_app your_script.py - Open the generated
your_app.specfile, and add arecursion_limitparameter to theAnalysissection (set it to a higher value like 5000):a = Analysis(['your_script.py'], # keep existing parameters like pathex, datas, etc. recursion_limit=5000) - Repack using the spec file:
pyinstaller your_app.spec
3. Explicitly Handle Openpyxl Imports
Sometimes PyInstaller's auto-detection misses nested openpyxl modules, triggering recursive scans. Add these to hiddenimports in your spec file:
hiddenimports=['openpyxl', 'openpyxl.cell', 'openpyxl.workbook', 'openpyxl.worksheet'],
This tells PyInstaller to include these modules directly instead of trying to recursively discover them.
4. Check Openpyxl Version Compatibility
Python 3.6.4 doesn't play well with openpyxl 3.0+ (which requires Python 3.7+). Make sure you're using a compatible version:
pip install openpyxl==2.6.4
This version is stable, supports Python 3.6, and works smoothly with PyInstaller 3.6.
5. Disable UPX Compression
UPX (the default compression tool in PyInstaller) can sometimes exacerbate recursion issues with complex libraries. Try packing without it:
pyinstaller --noupx your_script.py
I’ve helped several developers fix this exact error with these steps—start with upgrading PyInstaller first, as that’s the most common fix.
内容的提问来源于stack exchange,提问作者SGray2

