【问题标题】:Unable to import Pandas in AWS Lambda layer无法在 AWS Lambda 层中导入 Pandas
【发布时间】:2020-12-04 19:57:09
【问题描述】:

我将 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”目录,然后创建一个新层并上传压缩文件。

努力想知道我在哪里做空。

【问题讨论】:

  • 一位web search透露了几篇关于这个话题的文章。

标签: python amazon-web-services aws-lambda pip


【解决方案1】:

您是否考虑过为pandas 使用公开可用的层,例如来自这个流行的存储库:keithrozario/Klayers

例如,可用于 python 3.8 的层列表是 here 对应于 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
  1. 运行以下 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 控制台中基于 mypandaslayer.zip 创建 lambda 层。不要忘记将Compatible runtimes 指定为python3.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']

【讨论】:

  • 我正在使用它,它导致以下错误:“缺少可选依赖项'xlrd'。安装 xlrd >= 1.0.0 以获得 Excel 支持使用 pip 或 conda 安装 xlrd。”,
  • @MatthewMetros 好的。我将用如何制作这样的自定义层来更新答案。同时,您能否提供一些小的 lambda sn-p 代码用于测试以及您在层中还需要什么?
  • @MatthewMetros Answer 更新了如何专门为您的用例制作工作层。
  • 我对 Docker 一点也不熟悉。你建议我去哪里快速了解这个程序?
  • 对于在 Windows 上运行 Docker 命令时遇到“无效引用格式”错误的任何人: - 将“$PWD”更改为 ${pwd}(也不要使用引号) - 确保运行它位于路径不包含任何空格的文件夹中(并且要创建层压缩文件,请将 /dev/null 替换为 os.devnull)
【解决方案2】:

您也可以尝试使用 wheel 文件,而不是通过 pip 安装 pandas。这对我有用。只需下载所需的wheel文件,并将文件解压缩到您计划作为层上传的目录中,压缩目录,然后将其上传到lambda层。

Refer to this for step by step guide...

【讨论】:

    猜你喜欢
    • 2019-12-23
    • 2023-03-30
    • 2021-07-07
    • 2021-09-25
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    • 2021-01-09
    相关资源
    最近更新 更多