coder2505
coder2505

Reputation: 55

AWS Lambda python: Unable to import module 'lambda_function': No module named 'regex._regex'

I am currently working with AWS Lambda. Here is an excerpt of the code:

import pandas as pd
import re
import nltk
from stop_words import get_stop_words
stopwords = get_stop_words('en')
nltk.download('punkt')
nltk.download('wordnet')
wn = nltk.WordNetLemmatizer()

def lemmatization(txt):
    text = ([wn.lemmatize(word) for word in txt])
    return text

def lambda_handler(event,context):
    
        bucket = "aaabbb"
        key = "cccddd"
        s3_client = boto3.client('s3')
        s3_file = s3_client.get_object(Bucket=bucket, Key=key)
        s3_file_data = s3_file['Body'].read()
        s3_file_data = io.BytesIO(s3_file_data)
        df = pd.read_csv(s3_file_data)

        df['ABC'] = df['ABC'].apply(lambda x: lemmatization(x))
        print(df)

However, I am always getting the error:

Unable to import module 'lambda_function': No module named 'regex._regex'

I have already imported nltk and regex packages. Could you please help me with it?

Upvotes: 5

Views: 7999

Answers (4)

I have run into a similar issue. This is due to os compatibility issues. I am using a Mac. This has solved for me:

pip install regex --platform=manylinux2014_x86_64 --only-binary=:all: --target ./create_layer/lib/python3.11/site-packages # ( The location of where you want to download the package)

Upvotes: 0

pxeba
pxeba

Reputation: 1766

This is an unfixed issue. It's an OS compatibility problem

I tried switching the python versions both on lambda/windows but it didnt works. You can solve this issue using docker image or generating zip with linux. I tested doing the zip on ubuntu ec2 with python 3.10 and it worked

Upvotes: 0

Nachi Muthu
Nachi Muthu

Reputation: 41

I faced this problem just like you. The problem causing this error is the difference in the OS that you use and lambda function uses. When python installs a package it creates the installed files with respect to the OS you use. So when you use the deployment bundle that was created using a linux OS then it would work with lambda function.

There are many ways for a windows user to do this, but I would recommend using docker containers to install your packages.

Steps to do it:

  1. pull python:3.8 docker image (this is the highest version supported by lambda at the time of writing this answer)
  2. run your container with the directory with the code mounted to the container as volume.
  3. now inside the container navigate to the mounted folder and install your required packages using pip.
  4. come out of your container and now use these installed packages to build your bundle and deploy it on AWS lambda

ps: now when you execute your code on windows it will give an error because the packages installed are built for linux OS

Upvotes: 4

Jose Martinez
Jose Martinez

Reputation: 34

A possible solution could be that your OS uses a different version of Python (i.e. 3.6) when downloading your dependencies than your Lambda Function (i.e. 3.7). I would suggest attempting to download whatever version of Python you are using for your lambda script, and then for example if I wanted the version of Python to be 3.8 I would run the code:
pip3.8 install -r requirements.txt -t aws-lib .

Upvotes: 1

Related Questions