无法导入模块 ‘lambda_function’: 无法从 ‘lxml’ 导入名称 ‘etree’ aws lambda

huangapple go评论64阅读模式
英文:

Unable to import module 'lambda_function': cannot import name 'etree' from 'lxml' aws lambda

问题

我试图使用AWS Lambda函数。我需要使用lxml Python模块。

当我尝试导入时:

from lxml import html

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

AWS显示了一些错误。在这里:

{
    "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": []
}

当我尝试在本地PC上使用来自lxml的'html'时,该代码仅适用于Python 3.10版本。它只在conda环境和本地PC上的Python-3.10上运行。

  1. 我尝试安装更新的lxml模块。
  2. 尝试在AWS Lambda上使用3.10 Python版本。
  3. 在Docker Ubuntu上安装lxml,然后上传.zip文件。
  4. 在AWS上使用该模块作为层。

什么都不起作用。我该怎么办?

谢谢。

英文:

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.

答案1

得分: 1

这是因为 lxml 是一个依赖于操作系统的库。如果你在Macbook或Ubuntu上安装它,你会得到与Lambda运行时中使用的操作系统 (Amazon Linux 2) 不兼容的二进制文件。

为了解决这个问题,你需要生成与Amazon Linux 2 兼容的二进制文件。

  • 你可以使用预构建的打包 Docker 镜像,例如 lambci/lambda查看这里

  • 创建自己的自定义 Dockerfile,将 requirements.txt 文件复制到基于Amazon Linux的容器内,并运行 pip install -r requirements.txt。然后,你可以将Docker容器生成的Python包复制到主机(你的PC或CI/CD代理)上,并将其与你的Lambda代码一起压缩。你可以参考这个关于 Python 3.11 的示例。

英文:

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 are 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.

huangapple
  • 本文由 发表于 2023年6月29日 16:18:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定