使用PyInstaller 4.5打包含librdkafka 1.7.0的Python程序为EXE时出现confluent_kafka.libs未找到错误的求助
I've run into this exact issue before with confluent-kafka's delvewheel-packaged binaries on Windows. The problem stems from PyInstaller not automatically collecting the confluent_kafka.libs directory that contains the underlying librdkafka DLLs. Here's how to fix it:
Fix 1: Add the libs directory via PyInstaller command line
- First, locate the
confluent_kafka.libsfolder in your Python environment. If you're using a virtual env, it'll be atyour_env\Lib\site-packages\confluent_kafka\confluent_kafka.libs. For a global Python install, checkC:\Python38\Lib\site-packages\confluent_kafka\confluent_kafka.libs(adjust your Python version path as needed). - Modify your PyInstaller command to include this directory using the
--add-dataflag:pyinstaller --onefile --add-data "C:\Path\To\confluent_kafka.libs;confluent_kafka.libs" produce-simple-avro.py- On Windows, use a semicolon to separate the source path and destination path (the destination keeps the same folder name so the runtime code can find it).
Fix 2: Use a PyInstaller spec file (more maintainable)
If you plan to re-pack often, using a spec file is better:
- Generate a spec file for your script:
pyinstaller --onefile produce-simple-avro.py - Open the generated
produce-simple-avro.specfile in a text editor. Find thedataslist (it might be empty initially) and add an entry for the libs directory:a = Analysis( ['produce-simple-avro.py'], pathex=[], binaries=[], datas=[('C:\\Path\\To\\confluent_kafka.libs', 'confluent_kafka.libs')], # ... rest of the spec content remains the same ) - Re-pack using the spec file:
pyinstaller produce-simple-avro.spec
Why this happens
Confluent Kafka for Windows uses delvewheel to package its native librdkafka dependencies into the confluent_kafka.libs folder. At runtime, the library uses os.add_dll_directory to load these DLLs. PyInstaller doesn't recognize this non-standard directory structure by default, so it fails to include the folder in the packed executable. When you run the EXE, the temporary unpacked directory lacks confluent_kafka.libs, triggering the FileNotFoundError.
After applying either fix, run your EXE again—this should resolve the DLL loading issue and let your Avro producer run as expected.
内容的提问来源于stack exchange,提问作者Harshith Yadav

