首页 > 解决方案 > Heroku ImportError:无法找到 zbar 共享库

问题描述

我正在尝试制作一个可以通过我的不和谐机器人生成和读取二维码的系统。我的代码在我的编辑器中正常运行,但是ImportError: Unable to find zbar shared library一旦推送到heroku,它就会返回。我试过在这里以及在pyzbar 库上查找,但无济于事。我在运行 Python 3.9.4 64 位的 Windows 上,非常感谢任何帮助。

以下是完整的相关代码:

import discord
import pyqrcode
import re
import os
import shutil
import requests
import uuid
from discord.ext import commands
from PIL import Image
from pyzbar.pyzbar import decode

client = commands.Bot(command_prefix="p.")

class Bot(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.command(help="p.qr {link} or {QR code image}")
    async def qr(self, ctx, *, link=None):
        if link == None:
            try:
                url = ctx.message.attachments[0].url
            except IndexError:
                await ctx.reply("Invalid response. Please provide a `link` or a `QR code image`.", mention_author=False)
            else:
                if (url[0:26] == "https://cdn.discordapp.com"):
                    r = requests.get(url, stream=True)
                    imageName = (str(uuid.uuid4()) + ".jpg")
                    with open(imageName, "wb") as out_file:
                        shutil.copyfileobj(r.raw, out_file)
            decoded_image = str(decode(Image.open(imageName)))
            os.remove(imageName)
            filter = re.findall("'([^']*)'", (decoded_image))
            try:
                await ctx.reply(filter[0], mention_author=False)
            except IndexError:
                await ctx.reply("Invalid response. Please provide a `link` or a `QR code image`.", mention_author=False)
        else:
            open("QR.png", "x")
            qr = pyqrcode.create(link)
            qr.png("QR.png", scale=6)
            with open("QR.png", "rb") as file:
                await ctx.reply(file=discord.File(file, "QR.png"), mention_author=False)
            os.remove("QR.png")

def setup(client):
    client.add_cog(Bot(client))

来自 Heroku 的错误信息:

2021-07-15T21:44:32.535519+00:00 app[worker.1]: Traceback (most recent call last):
2021-07-15T21:44:32.535687+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
2021-07-15T21:44:32.536282+00:00 app[worker.1]:     spec.loader.exec_module(lib)
2021-07-15T21:44:32.536294+00:00 app[worker.1]:   File "<frozen importlib._bootstrap_external>", line 855, in exec_module
2021-07-15T21:44:32.536564+00:00 app[worker.1]:   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
2021-07-15T21:44:32.536781+00:00 app[worker.1]:   File "/app/cogs/bot.py", line 18, in <module>
2021-07-15T21:44:32.537046+00:00 app[worker.1]:     from pyzbar.pyzbar import decode
2021-07-15T21:44:32.537084+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/pyzbar/pyzbar.py", line 7, in <module>
2021-07-15T21:44:32.537332+00:00 app[worker.1]:     from .wrapper import (
2021-07-15T21:44:32.537342+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/pyzbar/wrapper.py", line 139, in <module>
2021-07-15T21:44:32.537563+00:00 app[worker.1]:     zbar_version = zbar_function(
2021-07-15T21:44:32.537566+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/pyzbar/wrapper.py", line 136, in zbar_function
2021-07-15T21:44:32.537871+00:00 app[worker.1]:     return prototype((fname, load_libzbar()))
2021-07-15T21:44:32.537872+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/pyzbar/wrapper.py", line 115, in load_libzbar
2021-07-15T21:44:32.538066+00:00 app[worker.1]:     libzbar, dependencies = zbar_library.load()
2021-07-15T21:44:32.538070+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.9/site-packages/pyzbar/zbar_library.py", line 65, in load
2021-07-15T21:44:32.538461+00:00 app[worker.1]:     raise ImportError('Unable to find zbar shared library')
2021-07-15T21:44:32.538704+00:00 app[worker.1]: ImportError: Unable to find zbar shared library

标签: pythonherokudiscord.pyimporterrorzbar

解决方案


推荐阅读