0

I am trying to make a system that can generate and read qr codes via my discord bot. My code functions properly in my editor however, it returns ImportError: Unable to find zbar shared library once pushed to heroku. I've tried looking here as well as on the pyzbar library to no avail. I'm on Windows running Python 3.9.4 64-bit, any help is appreciated greatly.

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:

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

1 Answers1

1

@ethanpartyof1, this may be late, but I have found a solution to make this work.

1. Add the APT buildpack to heroku

Run heroku buildpacks:add --index 1 heroku-community/apt in the app directory.

2. Make a file named Aptfile and add this to it:

libzbar0
libzbar-dev

After completing these 2 steps, commit and push. It should work without any hassles.

Sandy
  • 247
  • 3
  • 12