You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用PyInstaller打包Python文件遇RecursionError问题求助

Fixing RecursionError with PyInstaller Packaging

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 here
    
  • Disable UPX compression
    UPX (the default compression tool PyInstaller uses) can sometimes trigger recursion-related bugs during packaging. Try running your build command with the --noupx flag:

    pyinstaller --noupx your_script.py
    
  • Exclude 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-module flag 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.py
    
  • Fix 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-import to explicitly tell it which modules to include:

    pyinstaller --hidden-import numpy.core.multiarray your_script.py
    
  • Update 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

火山引擎 最新活动