librdkafka 1.7.0 の最新バージョンは、Windows 64 ビット OS の python(python 3.8.11) プログラムで使用すると正常に動作しますが、プログラムを 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!
再現方法
- Windows 10 64 ビット OS に python 3.8.11 をインストールします。
pip install confluent-kafka[avro]
- 次のコードでプログラムを作成します
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)
- exeファイルに変換するには、以下のコマンドを使用して「pyinstaller」== 4.5を使用します
pyinstaller --onefile name-of-file-with-above-code.py