You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PyInstaller 4.5打包含librdkafka 1.7.0的Python程序为EXE时出现confluent_kafka.libs未找到错误的求助

Solution for PyInstaller + confluent-kafka Avro FileNotFoundError on Windows

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

  1. First, locate the confluent_kafka.libs folder in your Python environment. If you're using a virtual env, it'll be at your_env\Lib\site-packages\confluent_kafka\confluent_kafka.libs. For a global Python install, check C:\Python38\Lib\site-packages\confluent_kafka\confluent_kafka.libs (adjust your Python version path as needed).
  2. Modify your PyInstaller command to include this directory using the --add-data flag:
    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:

  1. Generate a spec file for your script:
    pyinstaller --onefile produce-simple-avro.py
    
  2. Open the generated produce-simple-avro.spec file in a text editor. Find the datas list (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
    )
    
  3. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:03:11