1

在Windows 64位操作系统上的python(python 3.8.11)程序中使用最新版本的librdkafka 1.7.0时工作正常,但是当我将程序转换为exe文件并运行时,我收到以下错误

File "produce-simple-avro.py", line 1, in
File "", line 991, in _find_and_load
File "", line 975, in find_and_load_unlocked
File "", line 671, in load_unlocked
File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
File "confluent_kafka_init.py", line 18, in
File "confluent_kafka_init.py", line 9, in _delvewheel_init_patch_16682001180
File "os.py", line 1109, in add_dll_directory
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\Users\a\AppData\Local\Temp\_MEI132722\confluent_kafka.libs'
[13284] Failed to execute script 'produce-simple-avro' due to unhandled exception!

如何重现

  1. 在 Windows 10 64 位操作系统上安装 Python 3.8.11。
  2. pip install confluent-kafka[avro]
  3. 使用以下代码创建一个程序

from confluent_kafka import avro
from confluent_kafka.avro import AvroProducer


value_schema_str = """
{
   "namespace": "my.test",
   "name": "value",
   "type": "record",
   "fields" : [
     {
       "name" : "name",
       "type" : "string"
     }
   ]
}
"""

key_schema_str = """
{
   "namespace": "my.test",
   "name": "key",
   "type": "record",
   "fields" : [
     {
       "name" : "name",
       "type" : "string"
     }
   ]
}
"""

value_schema = avro.loads(value_schema_str)
key_schema = avro.loads(key_schema_str)
value = {"name": "Value"}
key = {"name": "Key"}


def delivery_report(err, msg):
    """ Called once for each message produced to indicate delivery result.
        Triggered by poll() or flush(). """
    if err is not None:
        print('Message delivery failed: {}'.format(err))
    else:
        print('Message delivered to {} [{}]'.format(msg.topic(), msg.partition()))

try:
    avroProducer = AvroProducer({
    'bootstrap.servers': 'localhost:9092',
    'on_delivery': delivery_report,
    'schema.registry.url': 'http://localhost:8081'
    }, default_key_schema=key_schema, default_value_schema=value_schema)
    while True:
        avroProducer.produce(topic='my_topic', value=value, key=key)
        avroProducer.flush()
        time.sleep(3)
except Exception as e:
    print(0)
  1. 为了转换为 exe 文件,我将使用 'pyinstaller' == 4.5,使用以下命令

pyinstaller --onefile name-of-file-with-above-code.py

4

0 回答 0