PyInstaller打包含scipy、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-importflags 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.pyThe 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 namedhook-scipy.pywith 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 pyinstallerunder "Location") or specify the hook folder when building:pyinstaller --additional-hooks-dir ./your-hooks-folder your_script.pyUpgrade 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-levelsklearnmodule, 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_submodulesin a spec file
First generate a spec file withpyinstaller your_test_script.py(this createsyour_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-isThen build using the modified spec file:
pyinstaller your_test_script.specForce collect all library files
Use the--collect-allflag 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

