PyInstaller打包.pyw文件为.exe时在「Appending archive to EXE」阶段触发FileNotFoundError问题求助
PyInstaller: FileNotFoundError When Appending Archive to EXE (File Exists But Can't Be Located)
The Problem
When trying to package a Python .pyw file into an .exe using PyInstaller, the main.exe file appears in the dist directory as expected. However, right after the log line:
17758 INFO: Appending archive to EXE D:\Code\Python\Plumber\dist\main.exe
A FileNotFoundError is thrown, claiming the system can't find the very file that was just created. Multiple attempts (adjusting command parameters, modifying the script) result in the same error. Full traceback is included below:
17758 INFO: Appending archive to EXE D:\Code\Python\Plumber\dist\main.exe Traceback (most recent call last): File "d:\code\python\_lib\lib\runpy.py", line 197, in _run_module_as_main return _run_code(code, main_globals, None, File "d:\code\python\_lib\lib\runpy.py", line 87, in _run_code exec(code, run_globals) File "D:\Code\Python\_lib\Scripts\pyinstaller.exe\__main__.py", line 7, in <module> File "d:\code\python\_lib\lib\site-packages\PyInstaller\__main__.py", line 126, in run run_build(pyi_config, spec_file, **vars(args)) File "d:\code\python\_lib\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs) File "d:\code\python\_lib\lib\site-packages\PyInstaller\building\build_main.py", line 815, in main build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build')) File "d:\code\python\_lib\lib\site-packages\PyInstaller\building\build_main.py", line 762, in build exec(code, spec_namespace) File "D:\Code\Python\Plumber\main.spec", line 23, in <module> exe = EXE(pyz, File "d:\code\python\_lib\lib\site-packages\PyInstaller\building\api.py", line 524, in __init__ self.__postinit__() File "d:\code\python\_lib\lib\site-packages\PyInstaller\building\datastruct.py", line 159, in __postinit__ self.assemble() File "d:\code\python\_lib\lib\site-packages\PyInstaller\building\api.py", line 782, in assemble set_exe_checksum(self.name) File "d:\code\python\_lib\lib\site-packages\PyInstaller\utils\win32\winutils.py", line 168, in set_exe_checksum pe = pefile.PE(exe_path) File "d:\code\python\_lib\lib\site-packages\pefile.py", line 1841, in __init__ self.__parse__(name, data, fast_load) File "d:\code\python\_lib\lib\site-packages\pefile.py", line 1884, in __parse__ stat = os.stat(fname) FileNotFoundError: [WinError 2] The system cannot find the file specified: 'D:\\Code\\Python\\Plumber\\dist\\main.exe'
Root Causes
Based on troubleshooting similar issues, here are the most likely culprits:
- Antivirus Real-Time Protection: Windows Defender or third-party antivirus tools often flag PyInstaller-generated EXEs as malicious and immediately quarantine/delete them. This happens right after the file is created, so the subsequent checksum step can't find it.
- Insufficient Permissions: Your user account might lack read/write permissions for the
distdirectory. Even though the file is created, PyInstaller can't access it later in the process. - Long File Path: Windows has a default path length limit of 260 characters. If your project path is close to or exceeds this, the system may fail to resolve the file path correctly.
- Version Compatibility Issues: Mismatched versions of PyInstaller and its dependency
pefilecan cause path parsing errors when calculating the EXE checksum.
Fixes to Try
Here are actionable solutions ordered by likelihood of success:
- Temporarily Disable Antivirus: Turn off real-time protection in Windows Defender (or your third-party antivirus) and re-run the PyInstaller command. Once the EXE is generated, add it to your antivirus's whitelist to prevent future issues.
- Run Command Line as Administrator: Right-click Command Prompt/PowerShell and select "Run as administrator", then execute your PyInstaller command. This ensures you have full permissions to access and modify files in the directory.
- Shorten the Project Path: Move your project folder to a shorter path (e.g., from
D:\Code\Python\PlumbertoD:\PyProjects\Plumber) to avoid hitting Windows' path length limit. - Update/Downgrade PyInstaller & pefile:
- Uninstall the current versions first:
pip uninstall -y pyinstaller pefile - Install a known-compatible version pair (e.g., PyInstaller 5.13.0 and pefile 2023.2.7):
pip install pyinstaller==5.13.0 pefile==2023.2.7
- Uninstall the current versions first:
- Skip Checksum Calculation in Spec File: If all else fails, modify your
main.specfile to skip the checksum step by addingchecksum=Falseandupx=False(UPX can sometimes complicate things):
Then re-run the build using the spec file:exe = EXE(pyz, a.scripts, a.binaries, a.zipfiles, a.datas, [], name='main', debug=False, bootloader_ignore_signals=False, strip=False, upx=False, upx_exclude=[], runtime_tmpdir=None, console=False, disable_windowed_traceback=False, target_arch=None, codesign_identity=None, entitlements_file=None, checksum=False) # Add this line to skip checksumpyinstaller main.spec
内容的提问来源于stack exchange,提问作者Tom Lee
相关产品推荐
相关产品推荐

