【问题标题】:Unable to import numpy 1.19.1 in AWS Lambda No module named 'numpy.core._multiarray_umath'无法在 AWS Lambda 中导入 numpy 1.19.1 没有名为“numpy.core._multiarray_umath”的模块
【发布时间】:2021-07-30 23:06:40
【问题描述】:

我无法使用 AWS Lambda 上的 python3.8 在 AWS Lambda 中导入 numpy 1.19.1

我正在使用以下依赖项:

  • 熊猫 1.1.0
  • pyarrow 1.0.0
  • numpy 1.19.1
  • psycopg2 2.8.5

因为我在windows环境下工作,所以我创建了一个安装python3.8的EC2 Linux实例并下载了所有需要的库,然后我将它们添加到项目中,但是当我尝试导入pandas时,我得到以下信息:

[ERROR] ImportError: Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.8 from "/var/lang/bin/python3.8",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.18.2" you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.

Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.

Original error was: No module named 'numpy.core._multiarray_umath'

Traceback (most recent call last):
  File "/var/task/src/py38-lib-test.py", line 28, in py38test
    import pandas
  File "/tmp/lib/pandas/__init__.py", line 16, in <module>
    raise ImportError(END RequestId: 07762380-1fc4)

最后,我注意到 AWS Lambda 提供了一个包含 numpy 和 sci-kit 的层,我尝试删除我的 numpy 版本但保留了其余部分并将该层添加到函数中,但发生了同样的错误。

在此先感谢您的 cmets。

【问题讨论】:

  • 如果您只使用 AWS 提供的层,而没有您的任何层,它可以工作吗?
  • 该库可能与当前的 lambda 运行时不兼容
  • @Marcin,它单独工作,但是当使用 pandas 和 pyarrow 时,它不起作用。我需要他们阅读 .parquet 文件

标签: linux pandas amazon-web-services numpy aws-lambda


【解决方案1】:

我使用Klayers提供的层来解决问题。

假设你在us-east-1区域运行python 3.8,根据this Klayers document,你可以使用arn:aws:lambda:ap-east-1:770693421928:layer:Klayers-python38-numpy:19作为你的层,这样你就可以在lambda函数中运行import numpy

【讨论】:

  • 这对我有用,但是在 sls py38 和 numpy=1.21.2 中设置它也不起作用
  • 请注意,目前我们没有 Klayers 提供的与 Python 3.9 兼容的 Pandas 层。如果您需要 Pandas,请使用 Python 3.8 运行时。
【解决方案2】:

AWS Lambda 函数不能以这种方式工作。如果你打开 Pandas 包,它会附带 Numpy 包,但它们不起作用。 简单的解决方案是首先根据您的python版本和工作环境从this站点分别下载所需的包,解压缩并将它们添加到您的项目目录中。创建项目的 .zip 并将其部署在 AWS Lambda 函数上。它会这样工作。 您可以参考this 站点以遵循完整的程序。

【讨论】:

    【解决方案3】:

    您的 ec2 实例是 amazon linux2 机器吗?您还可以尝试为 amazon linux 2 构建和运行 docker 映像,并通过卷挂载到您的主机来获得与您的 Lambda 中所需环境兼容的 python 库。

    类似于 docker lambda 的东西:

    https://github.com/lambci/docker-lambda/tree/master/python3.8

    【讨论】:

    • 我认为这是问题所在(linux版本),因为环境非常严格,这对我来说很复杂。我无法使用 docker,甚至无法正确获取软件。团队中的某个人以某种方式获得了正确的库,我们能够使用它们。感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2023-02-13
    • 2021-04-21
    • 2021-10-21
    • 1970-01-01
    • 2020-01-23
    • 2023-04-08
    • 2019-07-10
    • 2018-08-01
    相关资源
    最近更新 更多