导入numpy C扩展失败(Amplify)

4

GitHub上发布的交叉帖子。

我正在使用AWS Amplify和pipenv为我的Python 3.9 Lambda工作。我尝试使用pandas创建数据帧,进行一些处理,并将其写回CSV以供Sagemaker推理使用。

重现代码示例:

import pandas as pd

在这之后,代码立即失败。

错误信息:

以下是完整的错误信息:

[ERROR] Runtime.ImportModuleError: Unable to import module 'index': 

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.21.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'

我最初使用 pipenv install pandas 下载,这会自动安装numpy。

为了解决问题,我尝试了以下方法:

  • pipenv install numpy / pipenv uninstall numpy
  • pipenv uninstall pandas / pipenv install pandas
  • pipenv uninstall setuptools / pipenv install setuptools

需要注意的是,我使用的是Windows 10操作系统。

5个回答

3

我曾经遇到过类似的问题。经过大量研究,看起来由Amazon提供的AWSLambda-Python38-SciPy1x Lambda层是您最好的选择。更多信息在这里

您可以通过控制台手动添加该层,方法如下:图片参考


或者您可以通过Amplify CLI添加该层。 我在现有的lambda函数上运行了以下命令:

amplify function update
  • 选择你想要添加层的Lambda函数(无服务器函数)

  • 前往Lambda层配置

  • 提供现有Lambda层ARN下提供ARN。

  • 我使用了arn:aws:lambda:us-west-2:420165488524:layer:AWSLambda-Python38-SciPy1x:29

    amplify push

这样就可以解决问题了。


此外,以下是一些常用的 ARN 列表,可能对您有所帮助:https://github.com/keithrozario/Klayers/blob/master/deployments/python3.8/arns/us-east-1.csv - Michael Angelo
谢谢!有趣的是,这正是我在一点时间后想到的确切解决方案,现在已经运行了几天。虽然为了清晰起见,问题在于numpy版本太新,但我将您的解决方案标记为正确答案。理论上,这意味着您可以将旧版本的numpy添加到pipfile中并运行python 3.8。 - cmcnphp

2

调试时前往 numpy.core 可以帮助查看内部情况。

在我的情况下,造成此错误的原因是我创建的 Lambda Layer 中使用了错误平台的 .so 文件。

enter image description here (左侧为正确的文件,右侧为错误的文件)

我在我的 Mac OS 上制作了一个层,在那里 pip 默认下载适用于 darwin 平台的 numpy。但实际上,我需要下载适用于 Lambda 的 amazonlinux2 环境的软件包:

pip install \
 --platform=manylinux1_x86_64 \
 --only-binary=:all: \
 --target layer-dir/python \
 numpy;

1
对于numpy 1.24.3,我不得不使用以下方法来解决相同的问题:pip install \ --platform=manylinux2014_x86_64 \ --only-binary=:all: \ --target layer-dir/python \ numpy; (在Mac M1上构建,运行Mac OS Monterey) - martin_wun

0

我遇到了同样的问题,因为我在不同的Python版本中构建了软件包,而我尝试导入pandas层的AWS Lambda运行时。

因此,请检查您的Python版本,并在相同的Python版本中创建一个Lambda函数,然后添加您要创建的层。

这在我的情况下起作用了。


0

对我来说,切换到较低版本的Python是有效的。从Python3.8切换到Python3.7。


0

我在这个问题上浪费了好几个小时。你需要确保使用pip为Python 3.7版本构建时,在Lambda层中选择相同的支持平台。这对我起作用了。


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