PyInstaller打包启动屏程序生成超大文件夹问题求助
Hey there! Let's fix that absurdly large 630MB PyInstaller build for your splash screen app—way too bloated for such a simple tool. Here are practical, tested steps to slim it down drastically:
1. Optimize Your PyInstaller Build Commands First
Start with better parameters to avoid packing junk in the first place:
- Use
--onefile(if you can tolerate a tiny startup delay)
This bundles everything into a single compressed executable, which is almost always smaller than a folder build. Add--windowedto skip the console window (perfect for a splash screen):pyinstaller --onefile --windowed your_splash_script.py - Exclude unused dependencies explicitly
If you're using a GUI framework like PyQt/PySide, you're probably packing tons of unused modules (like QtWebEngine, QtMultimedia, etc.). Exclude them with--exclude-module:pyinstaller --windowed --exclude-module PyQt5.QtWebEngine --exclude-module PyQt5.QtMultimedia your_splash_script.py - Enable UPX compression
UPX shrinks binary files significantly. Install UPX first (it's free), then add the--upxflag (or--upx-dir /path/to/upxif it's not in your PATH):pyinstaller --windowed --upx your_splash_script.py - Clean up cached files
PyInstaller caches builds to speed things up, but old cache can add bloat. Use--cleanto start fresh:pyinstaller --clean --windowed your_splash_script.py
2. Post-Build Cleanup (For Your Existing dist Folder)
Since you already have the dist directory, here's how to trim it manually:
- Delete the entire
sharefolder
You noticed this is the biggest culprit—theme files are completely unnecessary for a standalone splash screen. Just delete thesharedirectory inside your dist folder; your app won't miss it. - Prune unused dynamic libraries
Most of the .so (Linux) or .dll (Windows) files aren't actually needed. To find which ones your app depends on:- On Linux: Run
ldd dist/your_app_executable | grep -o '/.*\.so'to get a list of required libraries. Delete any .so files in dist that aren't on this list. - On Windows: Use Dependency Walker (a free tool) to open your .exe and see which DLLs it actually uses. Delete all others.
- On Linux: Run
- Strip debug symbols
Remove unnecessary debug data from your executable and libraries:- Linux: Run
strip dist/your_app_executableandstrip dist/*.so - Windows: Use the
strip.exetool (from MinGW or Visual Studio) on your .exe and DLLs.
- Linux: Run
3. Cut Dependencies at the Source
The best way to reduce size is to use fewer dependencies in the first place:
- Use a minimal virtual environment
Create a fresh venv, install only what your splash screen needs (e.g., justpyqt5instead of the full PyQt5 suite, or even just Python's built-intkinterif your splash screen is simple). No extra packages = no extra bloat. - Switch to a lighter GUI library
If your splash screen doesn't need advanced features, ditch heavy frameworks like PyQt. Tkinter is built into Python, so it adds almost nothing to your build size. PySimpleGUI (a lightweight wrapper for tkinter) is also a great option if you want nicer visuals without the bloat. - Clean up your imports
Don't import entire modules if you only need one class. Instead ofimport PyQt5.QtWidgets, usefrom PyQt5.QtWidgets import QSplashScreen—this tells PyInstaller to skip packing unused parts of the library.
4. Advanced: Tweak the PyInstaller Spec File
For full control, generate and edit a spec file:
- Generate the spec file with:
pyinstaller --windowed your_splash_script.py - Open the generated
your_splash_script.specfile in a text editor. - Add more modules to the
excludeslist (e.g.,excludes=['PyQt5.QtWebEngine', 'PyQt5.QtMultimedia']). - Set
strip=Trueto automatically remove debug symbols during the build. - Only include necessary resource files in the
dataslist—don't let PyInstaller pack extra stuff.
With these steps, you should be able to get your splash screen app down to a much more reasonable size—often under 50MB even with Qt. Give them a shot and let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Luca




