-2

I've looked around but have had a hard time finding someone with a similar problem. Most of the articles I come across have a hard time deploying a package. My problem is that the package I uploaded has a hard time finding its resources.

For more context: I am using python3, I have used both RedHat 7 and Windows 10 to follow these instructions to create the layer I need, and I have successfully been able to follow them to create a layer and import it into my lambda_function.py file. The module I am trying to import is called qrcode. When the virtual environment is activated, I installed it with pip install qrcode[pil] on Windows 10 and pip3 install qrcode[pil] on my RedHat 7 ec2 instance. I have also tried to include a separate layer for pil itself, to which it still didn't work.

The error: The stacktrace is as follows -

{
  "errorMessage": "No module named 'Image'",
  "errorType": "ModuleNotFoundError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 30, in lambda_handler\n    img = qrcode.make(data)\n",
    "  File \"/opt/python/qrcode/main.py\", line 13, in make\n    return qr.make_image()\n",
    "  File \"/opt/python/qrcode/main.py\", line 296, in make_image\n    from qrcode.image.pil import PilImage\n",
    "  File \"/opt/python/qrcode/image/pil.py\", line 7, in <module>\n    import Image\n"
  ]
}
Shmack
  • 1,933
  • 2
  • 18
  • 23

1 Answers1

1

I got it to work, though I'm not 100% pleased with the answer.

Long story short, qrcode had an issue importing PIL. I came across this answer that talked about how lambda is missing some components in how it was built. So I installed my package (pip install qrcode) and imported it as a layer, then I used an arn specific for pil (talked about here), and it worked like a charm.

Shmack
  • 1,933
  • 2
  • 18
  • 23
  • The Lambda runtime environment doesn't generally include nonstandard Python packages or their native dependencies, as you would expect. This is no different to any installation of Linux, Windows, Mac etc. The problem here is most likely incompatibility between the runtime version of Python, the qrcode package, Pillow, and its dependencies. – jarmod Apr 05 '22 at 23:25
  • Yes. That's why they have layers. Sorry, if this comes off sarcastically, I'm just trying to follow. I wasn't expecting for pillow to be installed on lambda. I was expecting it to be installed with the qrcode package, which it was. The qrcode library had a problem importing Pillow, which can be installed jointly with the qrcode library with the pip install qrcode[pil] command. There is a try, except statement written into the import statement, and since it fails to import the module, it means that pil isn't properly installed, or like you said, in the proper runtime environment. @jarmod – Shmack Apr 06 '22 at 03:03