简体   繁体   English

Heroku ImportError: 找不到 zbar 共享库

[英]Heroku ImportError: Unable to find zbar shared library

I am trying to make a system that can generate and read qr codes via my discord bot.我正在尝试制作一个可以通过我的 discord 机器人生成和读取二维码的系统。 My code functions properly in my editor however, it returns ImportError: Unable to find zbar shared library once pushed to heroku.我的代码在我的编辑器中正常运行,但是,它返回ImportError: Unable to find zbar shared library一旦推送到 heroku。 I've tried looking here as well as on the pyzbar library to no avail.我试过在这里以及在pyzbar 库上查找,但无济于事。 I'm on Windows running Python 3.9.4 64-bit, any help is appreciated greatly.我在 Windows 上运行 Python 3.9.4 64 位,非常感谢任何帮助。

Below is the full relevant code:以下是完整的相关代码:

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))

And the error message from Heroku:以及来自 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

I ran into the same issue as you and what I found that worked for me is as follows:我遇到了和你一样的问题,我发现对我有用的内容如下:

First, make sure the Windows 64-bit wheel build of ZBar is installed on your machine.首先,确保您的机器上安装了 ZBar 的 Windows 64 位轮构建。 You can do this by going to the patched clone of the ZBar library found here :您可以通过转到此处找到的 ZBar 库的修补克隆来执行此操作

If this still doesn't work, try using qrtools to read the QR code.如果这仍然不起作用,请尝试使用qrtools读取 QR 码。 For example, a simple implementation may be as follows:例如,一个简单的实现可能如下:

from qrtools.qrtools import QR
qr = qrtools.QR()
qr.decode("test.png")
print qr.data

@ethanpartyof1, this may be late, but I have found a solution to make this work. @ethanpartyof1,这可能会迟到,但我找到了一个解决方案来完成这项工作。

1. Add the APT buildpack to heroku 1.将APT buildpack添加到heroku

Run heroku buildpacks:add --index 1 heroku-community/apt in the app directory.在应用目录中运行heroku buildpacks:add --index 1 heroku-community/apt

2. Make a file named Aptfile and add this to it: 2. 创建一个名为Aptfile的文件并将其添加到其中:

libzbar0
libzbar-dev

After completing these 2 steps, commit and push.完成这两个步骤后,提交并推送。 It should work without any hassles.它应该没有任何麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM