Numpy导入错误和Aws lambda层文件夹结构(导入Numpy C扩展失败)



问题

你好,

我一直在尝试创建两个lambda层,分别包含numpy(1.23.2(和pandas(1.4.4(包,然后将它们与lambda函数一起使用。(注意:我还在熊猫层pytz-2022.2.1中插入

然而,当我通过UI测试它时,我得到了以下错误:

[ERROR] ImportError: 
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "/var/lang/bin/python3.9"
* The NumPy version is: "1.23.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'

我一直在遵循aws指南,文章和其他文章的堆栈溢出,但没有成功。

步骤

我使用这个链接从pypi numpy-1.23.2-cp311-cp311-manylinux_2_17_x86_64.manylinux214_x86_64whl 获得numpy包

我解压缩.whl并将内容压缩到一个新的.zip文件夹中。这个新zip的结构如下:

numpy-layer.zip
└─python
└─numpy
└─numpy-1.23.2.dist-info
└─numpy.libs

(需要明确的是,当我提取文件时,我会得到一个名为python的文件夹(

我把拉链推到s3桶里:

aws s3 cp  $dir/lambda-numpy-layer.zip s3://my-bucket/lambda_packages/numpy.zip

我发布了一个新的层版本:

aws lambda publish-layer-version 
--layer-name my-numpy-layer  
--description "Using numpy 1.23.2"  
--content S3Bucket=my-bucket,S3Key=lambda_packages/numpy.zip 
--output json

并更新lambda函数配置以使用新层:

aws lambda update-function-configuration 
--function-name my-function-name 
--layers  $numpy_layer_arn_with_version 
$pandas_layer_arn_with_version

(注意,我检查了aws-cli命令是否有问题,所以我尝试通过UI将zip上传到lambda层,但错误仍然相同(

调试

在尝试调试时,我打印出了一些文件夹结构。


def lambda_handler(event, context):

import os
print("Current dir", os.getcwd())
print("listdit /root", os.listdir("/"))
print("listdir /opt", os.listdir("/opt"))
print("listdir /opt/python", os.listdir("/opt/python"))
print("listdir /var/lang/bin/", os.listdir('/var/lang/bin/'))
print("PYTHONPATH:", os.environ.get('PYTHONPATH'))
print("PATH:", os.environ.get('PATH'))
import numpy
return {
'headers': {'Content-Type': 'application/json'}
, 'statusCode': 200
, 'body': body
}

哪个输出:

Current dir /var/task
listdit /root ['proc', 'media', 'sys', 'sbin', 'lib64', 'tmp', 'opt', 'dev', 'mnt', 'root', 'etc', 'home', 'bin', 'usr', 'var', 'run', 'boot', 'lambda-entrypoint.sh', 'lib', 'srv', 'THIRD-PARTY-LICENSES.txt']
listdir /opt ['python']
listdir /opt/python ['numpy', 'numpy-1.23.2.dist-info', 'numpy.libs', 'pandas', 'pandas-1.4.4.dist-info', 'pandas.libs', 'pytz', 'pytz-2022.2.1.dist-info']
listdir /var/lang/bin/ ['pydoc3.9', 'pydoc3', 'python3.9-config', 'python-config', 'python3.9', 'pip', 'python3-config', 'pip3.9', 'pydoc', 'pip3', 'python', 'python3']
PYTHONPATH: /var/runtime
PATH: /var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin
[ERROR] ImportError: 
...

可能是路径指向/opt/bin,但我的包直接位于/opt/下?否则,我的压缩文件夹结构会出现问题。

通过这种方式进行调试,您不太可能找到所需的结构。幸运的是,你不必这么做。

相反,我建议您在本地将所需的库安装到您命名为"的目录中;python";使用CCD_ 1,然后压缩该目录。皮普会为你做所有的工作。

首先,检查您是否激活了合适版本的python:

$ python -V
Python 3.9.5

制作一个";python";目录:

$ mkdir python

然后安装您需要的:

$ pip install pandas==1.4.4 -t ./python --no-cache-dir
Collecting pandas==1.4.4
...
...
...
Successfully installed numpy-1.23.2 pandas-1.4.4 python-dateutil-2.8.2 pytz-2022.2.1 six-1.16.0

请注意,只需pip以这种方式安装pandas,就可以获得所需的numpy的相应版本,以及其他一些所需的依赖项

让我们看看我们得到了什么:

$ tree ./python -L 1
./python/
├── bin
├── dateutil
├── numpy
├── numpy-1.23.2.dist-info
├── numpy.libs
├── pandas
├── pandas-1.4.4.dist-info
├── __pycache__
├── python_dateutil-2.8.2.dist-info
├── pytz
├── pytz-2022.2.1.dist-info
├── six-1.16.0.dist-info
└── six.py
12 directories, 1 file

这是一堆目录,好在我们不必手动构建所有这些!

让我们把它包起来。

$ zip -r pandas-1-4-4-py-39-layer.zip ./python

就是这样!现在,您只需要使用UI或CLI将zip上传到您的层,就可以开始了。

最新更新