使用PyInstaller打包Python文件遇RecursionError问题求助
Hey there, let's work through this recursion depth issue you're hitting when using PyInstaller. Just bumping the recursion limit to 1500 might not cut it because this error often stems from PyInstaller's dependency analysis process, not just your script's runtime recursion. Here are some actionable steps to try:
Crank up the recursion limit even higher
Sometimes 1500 isn't enough for PyInstaller's deep dependency scans. Try setting it to a larger value at the very top of your main script (before any other imports):import sys sys.setrecursionlimit(5000) # Rest of your imports and code follow hereDisable UPX compression
UPX (the default compression tool PyInstaller uses) can sometimes trigger recursion-related bugs during packaging. Try running your build command with the--noupxflag:pyinstaller --noupx your_script.pyExclude problematic modules
If your code uses modules with circular dependencies or dynamic imports, PyInstaller's auto-scan might get stuck in a loop. Use the--exclude-moduleflag to skip those modules (you'll need to manually ensure any critical dependencies are still included):pyinstaller --exclude-module module_with_recursion_issues your_script.pyFix circular imports in your code
Double-check if your own modules have circular imports (e.g., Module A imports Module B, which imports Module A). These loops can throw off PyInstaller's dependency analyzer. Refactor your code to remove these cycles—for example, move shared code to a third module or use lazy imports.Manually specify hidden imports
For large libraries like NumPy or Pandas, PyInstaller might miss some submodules and get stuck in recursive scans. Use--hidden-importto explicitly tell it which modules to include:pyinstaller --hidden-import numpy.core.multiarray your_script.pyUpdate PyInstaller to the latest version
Older versions of PyInstaller had known bugs with recursion during dependency analysis. Run this command to upgrade:pip install --upgrade pyinstaller
Start with the first couple of steps and work your way down—chances are one of these will resolve the recursion error for you.
内容的提问来源于stack exchange,提问作者user3064089




