问题描述
我试图在AWS Lambda中使用LXML模块,但没有运气.我使用以下命令下载了LXML:
I am trying to use the LXML module within AWS Lambda and having no luck. I downloaded LXML using the following command:
pip install lxml -t folder
要将其下载到我的lambda函数部署包中.与其他所有lambda函数一样,我压缩了lambda函数的内容,并将其上传到AWS Lambda.
To download it to my lambda function deployment package. I zipped the contents of my lambda function up as I have done with all other lambda functions, and uploaded it to AWS Lambda.
但是不管我怎么努力,我得到这个错误,当我运行函数:
However no matter what I try I get this error when I run the function:
Unable to import module 'handler': /var/task/lxml/etree.so: undefined symbol: PyFPE_jbuf
当我在本地运行它时,没有任何问题,只是当我在Lambda上运行时才出现此问题.
When I run it locally, I don't have an issues, it is simply when I run in on Lambda where this issue arises.
推荐答案
我遇到了同样的问题.
I faced the same issue.
RaphaëlBraud 发布的链接很有帮助,而这个链接也是如此: https://nervous.io/python/aws/拉姆达/2016/02/17/SciPy的-大熊猫 - 拉姆达/
The link posted by Raphaël Braud was helpful and so was this one:https://nervous.io/python/aws/lambda/2016/02/17/scipy-pandas-lambda/
使用两个链接,我能够成功导入lxml和其他必需的软件包.这是我遵循的步骤:
Using the two links I was able to successfully import lxml and other required packages. Here are the steps I followed:
- 使用Amazon Linux ami启动ec2机器
-
运行以下脚本来累积依赖关系:
- Launch an ec2 machine with Amazon Linux ami
Run the following script to accumulate dependencies:
set -e -o pipefail
sudo yum -y upgrade
sudo yum -y install gcc python-devel libxml2-devel libxslt-devel
virtualenv ~/env && cd ~/env && source bin/activate
pip install lxml
for dir in lib64/python2.7/site-packages \
lib/python2.7/site-packages
do
if [ -d $dir ] ; then
pushd $dir; zip -r ~/deps.zip .; popd
fi
done
mkdir -p local/lib
cp /usr/lib64/ #list of required .so files
local/lib/
zip -r ~/deps.zip local/lib
创建链接.样本文件内容:
Create handler and worker files as specified in the link. Sample file contents:
handler.py
handler.py
import os
import subprocess
libdir = os.path.join(os.getcwd(), 'local', 'lib')
def handler(event, context):
command = 'LD_LIBRARY_PATH={} python worker.py '.format(libdir)
output = subprocess.check_output(command, shell=True)
print output
return
worker.py:
worker.py:
import lxml
def sample_function( input_string = None):
return "lxml import successful!"
if __name__ == "__main__":
result = sample_function()
print result
- 将处理程序和辅助程序添加到zip文件中.
下面是如何zip文件看起来上述步骤之后的结构:
Here is how the structure of the zip file looks after the above steps:
deps
├── handler.py
├── worker.py
├── local
│ └── lib
│ ├── libanl.so
│ ├── libBrokenLocale.so
| ....
├── lxml
│ ├── builder.py
│ ├── builder.pyc
| ....
├── <other python packages>
- 确保在创建lambda函数时指定了正确的处理程序名称.在上面的示例中,它将是-"handler.handler"
希望这会有所帮助!
这篇关于AWS Lambda不导入LXML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!