无法在AWS Lambda层中导入Pandas

4
我将 Python 的 pandas 库上传至 Lambda 后,运行 Lambda 时出现以下错误:
"errorMessage": "Unable to import module 'lambda_function': C extension: No module named 'pandas._libs.interval' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first."

我真的不确定这里应该发生什么。但是为了更多的上下文,我创建了一个名为“python”的目录,然后在我的终端上运行了以下命令:

python3.8 -m pip install pandas -t .

我将“python”目录压缩,并创建一个新层,上传这个 zip 文件。

我不太清楚自己现在的进展情况。


一次网络搜索揭示了关于这个主题的几篇文章。 - John Rotenstein
3个回答

8

你是否考虑过使用的公开层,例如来自这个流行库:keithrozario/Klayers

例如,python 3.8可用的层列表在此处us-east-1)。

在这种情况下,对于us-east-1,你可以使用以下命令添加pandas层:

arn:aws:lambda:us-east-1:770693421928:layer:Klayers-python38-pandas:16

更新,制作自定义层

我使用 pandasxlrd 创建了自定义层,并确认它可行。

所使用的技术包括最近 AWS 博客 中描述的docker 工具

因此,对于此问题,我进行了如下验证:

  1. 创建一个空文件夹,例如 mylayer

  2. 进入文件夹并创建 requirements.txt 文件,并将其内容填充为

pandas
xlrd

运行以下docker命令:
docker run -v "$PWD":/var/task "lambci/lambda:build-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages/; exit"
  1. 创建zip格式的层:
zip -r mypandaslayer.zip python > /dev/null
  1. 在AWS控制台上基于创建lambda层。不要忘记指定Compatible runtimespython3.8

  2. 使用以下lambda函数在lambda中测试该层:

import json

import pandas
import xlrd

def lambda_handler(event, context):
    
    print(dir(pandas))
    print(dir(xlrd))

该函数执行正确:
['BooleanDtype', 'Categorical', 'CategoricalDtype', 'CategoricalIndex', 'DataFrame', 'DateOffset', 'DatetimeIndex', 'DatetimeTZDtype', 'ExcelFile', 'ExcelWriter', 'Float64Index', 'Grouper', 'HDFStore', 'Index', 'IndexSlice', 'Int16Dtype', 'Int32Dtype', 'Int64Dtype', 'Int64Index', 'Int8Dtype', 'Interval', 'IntervalDtype', 'IntervalIndex', 'MultiIndex', 'NA', 'NaT', 'NamedAgg', 'Period', 'PeriodDtype', 'PeriodIndex', 'RangeIndex', 'Series', 'SparseDtype', 'StringDtype', 'Timedelta', 'TimedeltaIndex', 'Timestamp', 'UInt16Dtype', 'UInt32Dtype', 'UInt64Dtype', 'UInt64Index', 'UInt8Dtype', '__builtins__', '__cached__', '__doc__', '__docformat__', '__file__', '__getattr__', '__git_version__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_config', '_hashtable', '_is_numpy_dev', '_lib', '_libs', '_np_version_under1p16', '_np_version_under1p17', '_np_version_under1p18', '_testing', '_tslib', '_typing', '_version', 'api', 'array', 'arrays', 'bdate_range', 'compat', 'concat', 'core', 'crosstab', 'cut', 'date_range', 'describe_option', 'errors', 'eval', 'factorize', 'get_dummies', 'get_option', 'infer_freq', 'interval_range', 'io', 'isna', 'isnull', 'json_normalize', 'lreshape', 'melt', 'merge', 'merge_asof', 'merge_ordered', 'notna', 'notnull', 'offsets', 'option_context', 'options', 'pandas', 'period_range', 'pivot', 'pivot_table', 'plotting', 'qcut', 'read_clipboard', 'read_csv', 'read_excel', 'read_feather', 'read_fwf', 'read_gbq', 'read_hdf', 'read_html', 'read_json', 'read_orc', 'read_parquet', 'read_pickle', 'read_sas', 'read_spss', 'read_sql', 'read_sql_query', 'read_sql_table', 'read_stata', 'read_table', 'reset_option', 'set_eng_float_format', 'set_option', 'show_versions', 'test', 'testing', 'timedelta_range', 'to_datetime', 'to_numeric', 'to_pickle', 'to_timedelta', 'tseries', 'unique', 'util', 'value_counts', 'wide_to_long']
['Book', 'FMLA_TYPE_ARRAY', 'FMLA_TYPE_CELL', 'FMLA_TYPE_COND_FMT', 'FMLA_TYPE_DATA_VAL', 'FMLA_TYPE_NAME', 'FMLA_TYPE_SHARED', 'MMAP_AVAILABLE', 'Operand', 'Ref3D', 'USE_MMAP', 'X12Book', 'XLDateError', 'XLRDError', 'XL_CELL_BLANK', 'XL_CELL_BOOLEAN', 'XL_CELL_DATE', 'XL_CELL_EMPTY', 'XL_CELL_ERROR', 'XL_CELL_NUMBER', 'XL_CELL_TEXT', '__VERSION__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'biff_text_from_num', 'biffh', 'book', 'cellname', 'cellnameabs', 'colname', 'compdoc', 'count_records', 'decompile_formula', 'dump', 'dump_formula', 'empty_cell', 'error_text_from_code', 'evaluate_name_formula', 'formatting', 'formula', 'info', 'mmap', 'oBOOL', 'oERR', 'oNUM', 'oREF', 'oREL', 'oSTRG', 'oUNK', 'okind_dict', 'open_workbook', 'os', 'pprint', 'rangename3d', 'rangename3drel', 'sheet', 'sys', 'timemachine', 'xldate', 'xldate_as_datetime', 'xldate_as_tuple', 'xlsx', 'zipfile']

1
@MatthewMetros的回答已更新,介绍了如何为您的使用情况制作工作层。 - Marcin
1
我完全不熟悉Docker。你有什么建议让我快速了解这个程序? - Matthew Metros
@MatthewMetros 我无法运行你的代码,因为我没有你的 Excel 文件。也许你可以提供一个不需要自定义 Excel 文件的示例? - Marcin
1
对于在Windows上运行Docker命令时遇到“无效的引用格式”错误的任何人:
  • 将“$PWD”更改为${pwd}(也不要使用倒引号)
  • 确保在路径中不包含任何空格的文件夹中运行它
(并且要创建层压缩文件,请将/dev/null替换为os.devnull)
- Sizigia
谢谢你的回答。Klayers 对我很有用。我尝试通过下载 pandas wheel,解压缩,然后重新压缩并将其上传为自定义层来创建自己的层,但它不起作用。最终 Klayers(numpy 和 pandas)对我有用。 - Silly Sally
显示剩余4条评论

3

如果您正在AWS上使用pandas,最好使用AWS SDK for pandas(AWS Datawrangler)而不是安装pandas或导入第三方层。

您可以通过点击以下链接了解更多有关安装的信息:

https://aws-sdk-pandas.readthedocs.io/en/stable/install.html

只需从AWS层中添加图层: 输入图像描述

或通过其ARN:
arn:aws:lambda:<region>:336392948345:layer:AWSSDKPandas-Python<python-version>:<layer-version>.

例如:

arn:aws:lambda:us-east-1:336392948345:layer:AWSSDKPandas-Python37:1.

ARN的完整列表可以在这里找到。


这个图层的尺寸是多少? - undefined

0

如果您无法通过pip安装pandas,也可以尝试使用wheel文件。这对我很有效。只需下载所需的wheel文件,将文件解压缩到您计划上传为层的目录中,将目录压缩为zip文件,并将其上传到lambda层。

请参考此处的逐步指南...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接