PyInstaller生成exe报错‘failed to execute script’求助
Let's break down the possible issues and fixes based on your spec file and problem description:
1. First, get the actual error message (most critical step)
The generic "Failed to execute script" error doesn't give us enough detail to diagnose the problem. To see the real traceback:
- Modify your
EXEblock in the spec file to setconsole=True:exe = EXE(pyz, a.scripts, [], exclude_binaries=True, name='simulator', debug=False, bootloader_ignore_signals=False, strip=False, upx=True, console=True , icon='bin\\ui\\icon.ico') - Rebuild the exe with
pyinstaller simulator.spec - Run the generated exe again—this time a console window will pop up showing the exact error (like a missing module, file not found, or syntax issue). This is the fastest way to pinpoint the root cause.
2. Fix path-related issues in your spec file
Windows paths require proper handling, and PyInstaller is strict about path formatting:
- Pathex: Your
pathex=['C:\Simulator']uses an unescaped backslash, which can cause unexpected parsing. Change it to a raw string or use forward slashes:pathex=[r'C:\Simulator'] # Raw string avoids escape issues # OR pathex=['C:/Simulator'] - Icon path: The same escape problem applies to
'bin\ui\icon.ico'—escape the backslashes or use forward slashes:icon='bin\\ui\\icon.ico' # OR icon='bin/ui/icon.ico' - Datas collection: The
**wildcard might not work as expected in PyInstaller's datas configuration. Since you're working with.kvfiles (likely a Kivy app), try specifying the exact subdirectory instead of using recursive wildcards:
Also, double-check that yourdatas=[('bin/ui/*.kv', 'bin/ui'), ('bin/ui/*.xml', 'bin/ui')]simulator.pywloads these files using relative paths that match the structure in thedist/simulatorfolder after packaging.
3. Check for missing hidden imports
PyInstaller often fails to auto-detect dependencies for frameworks like Kivy. Update your hiddenimports list to include modules your app relies on. For a basic Kivy app, this might look like:
hiddenimports=['kivy.lang.builder', 'kivy.uix.boxlayout', 'kivy.uix.button'] # Add modules your app uses
You can also run an initial build with pyinstaller --hidden-import=MODULE_NAME simulator.pyw to test missing imports, then add them to your spec file permanently.
4. Verify file permissions and location
- Try running the exe from a non-restricted directory (like your Desktop) instead of
Program Filesor other protected folders—Windows permissions can block apps from accessing required files. - After packaging, check if all
.kvand.xmlfiles are present indist/simulator/bin/ui. If they're missing, your datas configuration is incorrect.
5. Disable UPX temporarily
UPX compression can sometimes break certain binaries or scripts. Try rebuilding with upx=False in both EXE and COLLECT blocks to rule out this issue:
exe = EXE(pyz, a.scripts, [], exclude_binaries=True, name='simulator', debug=False, bootloader_ignore_signals=False, strip=False, upx=False, console=True , icon='bin\\ui\\icon.ico') coll = COLLECT(exe, a.binaries, a.zipfiles, a.datas, strip=False, upx=False, upx_exclude=[], name='simulator')
Once you get the actual error from the console, you'll be able to narrow down the problem even further!
内容的提问来源于stack exchange,提问作者Treex

