skip to Main Content

I was trying to use aws lambda functions. I needed to use lxml python module.

when I try to import:

from lxml import html

def lambda_handler(event,context):
    return 'okay'

aws shows some errors. Here:

{
"errorMessage": "Unable to import module 'lambda_function': cannot import name 'etree' from 'lxml' (/var/task/lxml/__init__.py)",
 "errorType": "Runtime.ImportModuleError",
 "requestId": "426b7f93-d703-4d7d-9bda-86cbfdf85fe2",
 "stackTrace": []
}

When I was trying to use the ‘html’ from lxml on local PC. The code was working on python 3.10 version only. It worked with conda environment and local PC on python-3.10 only.

  1. I tried to install the updated lxml module.
  2. Tried 3.10 python version on AWS Lambda
  3. Installing the lxml on Docker Ubuntu. then upload the .zip
  4. Using the module as layer on AWS.

Nothing works. What can I do?

Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    I finally found a solution provided by the aws team themselves. Actually, Allan Chua was right. The issue is with the Operating system. So, if you download the modules/libraries in the AWS server and then use them in lambda functions, it will not have any issues.

    Follow this post and video: https://repost.aws/knowledge-center/lambda-import-module-error-python

    video: https://www.youtube.com/watch?v=u6rh4YzhrTg&t=27s


  2. This is happening because lxml is an OS-dependent library. If you install it using your macbook or ubuntu, you will get binaries that are going to be incompatible with the OS used inside the Lambda runtime which is (Amazon Linux 2).

    To solve this, you will have to generate binaries that were compatible with Amazon Linux 2.

    • You can either use a pre-built packaging Docker image like lambci/lambda (Check here)
    • Create your own custom Dockerfile that will copy the requirements.txt file inside the Amazon Linux-based container
      and run the pip install -r requirements.txt. You can then copy the python packages produced by the Docker container into the host machine (Your PC or the CI/CD agent) and zip it with your code to Lambda. You can take a look at this example for Python 3.11.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search