Why does the AWS Lambda function not recognize the regex library?

214 Views Asked by At

I am developing an AWS Lambda function where I parse some input with the regex library. Here are the code snippets where it is used:

import regex

def lambda_handler(event, context):
    
    ...

    pattern = r'\{(\w+)\}'
    text_variables = regex.findall(pattern, mail_body)
    for t_var in text_variables:
        if t_var not in variables:
            error_list.append(f"An unregistered variable name was used in the mail body, specifically {t_var}.")

When testing the function, it fails with the error message (the name of the file containing the lambda handler is app.py):

{
  "errorMessage": "Unable to import module 'app': No module named 'regex._regex'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "XXXXX",
  "stackTrace": []
}

First, the runtime settings do say that the handler is app.lambda_handler .

I have tried adding a layer, by downloading the regex library in a folder called 'python', zipping it and uploading it. I have also tried using a folder structure such as python/lib/python3.10/site-packages/{Libraries}. I even tried using specifically the manylinux version, knowing that Lambda runs on Linux.

I have also tried simply downloading the library in the deployment package itself, something that I did in the past and it worked.

Yet, whether I use just one of these "solutions" or both at the same time, the result is the same.

I am using the Python 3.10 runtime.

EDIT: I have played around a bit more and it seems like the Layer does do something, as before adding it it says it cannot find module regex, and after adding it it says it cannot find regex._regex; I know that _regex is a module used in regex, but is it a standalone? What can I do to include it in my dependencies?

1

There are 1 best solutions below

0
Spiridon On

As per the first comment, I simply used re.findall()

It solved my problem, though I would still want to know the root of the problem, as I'm assuming that regex might still be better sometimes (otherwise why would anyone use it?).