You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包含scipy、sklearn程序时导入错误及警告解决问询

Fixing PyInstaller Packaging Issues with Scipy, Pandas, and Sklearn

Got it, let's work through your two PyInstaller problems—these are super common when dealing with scientific Python libraries, so I’ve got a few tried-and-true fixes for you.

1. Resolving "hidden imports not found!" Warning with Scipy

Scipy relies heavily on dynamic imports that PyInstaller’s auto-detection often misses, which triggers that annoying warning. Here’s how to tackle it:

  • Manually specify missing hidden imports via command line
    When running PyInstaller, add --hidden-import flags for the specific scipy submodules listed in the warning. For example, common missing ones include:

    pyinstaller --hidden-import scipy.special._ufuncs_cxx --hidden-import scipy.linalg._fblas your_script.py
    

    The warning message will usually name the exact submodules that are missing, so use those to target your flags.

  • Create a custom hook file for scipy
    Make a file named hook-scipy.py with this code to auto-collect all scipy submodules:

    from PyInstaller.utils.hooks import collect_submodules
    hiddenimports = collect_submodules('scipy')
    

    Place this file in PyInstaller’s default hooks directory (find it via pip show pyinstaller under "Location") or specify the hook folder when building:

    pyinstaller --additional-hooks-dir ./your-hooks-folder your_script.py
    
  • Upgrade your libraries
    Sometimes the warning comes from version mismatches. Update PyInstaller and Scipy to their latest stable versions:

    pip install --upgrade pyinstaller scipy
    

2. Fixing Import Errors for Sklearn and Scipy in the Built EXE

Even if the build finishes without errors, PyInstaller often fails to catch dynamic imports from sklearn and scipy. Let’s fix this for your test script:

  • Explicitly import sklearn submodules in your code
    PyInstaller only tracks imports that are explicitly called. Your test script only imports the top-level sklearn module, but sklearn’s core functionality lives in submodules. Add imports for the submodules your actual program uses (even if you don’t call them directly) to force PyInstaller to include them. For example:

    import time
    import pandas as pd
    import sklearn
    # Add explicit imports for sklearn submodules you rely on
    import sklearn.ensemble
    import sklearn.preprocessing
    
    def main():
        print('hello world!')
        time.sleep(3) # Replace ... with a valid number like 3
    
    if __name__ == "__main__":
        main()
    
  • Use collect_submodules in a spec file
    First generate a spec file with pyinstaller your_test_script.py (this creates your_test_script.spec). Edit the spec file to auto-collect all submodules from sklearn, scipy, and pandas:

    from PyInstaller.utils.hooks import collect_submodules
    
    a = Analysis(
        ['your_test_script.py'],
        pathex=[],
        binaries=[],
        datas=[],
        hiddenimports=collect_submodules('sklearn') + collect_submodules('scipy') + collect_submodules('pandas'),
        hookspath=[],
        hooksconfig={},
        runtime_hooks=[],
        excludes=[],
        noarchive=False,
    )
    # Keep the rest of the spec file content as-is
    

    Then build using the modified spec file:

    pyinstaller your_test_script.spec
    
  • Force collect all library files
    Use the --collect-all flag to tell PyInstaller to include every file from pandas, sklearn, and scipy—this is a brute-force but reliable fix:

    pyinstaller --collect-all pandas --collect-all sklearn --collect-all scipy your_test_script.py
    

Quick Debug Tip

Always run your EXE from the command prompt instead of double-clicking it—this will show you the exact missing module in the error message, so you can target your fixes more precisely.

内容的提问来源于stack exchange,提问作者Luke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:00:05