无法在 AWS Lambda 中导入 numpy 1.19.1 没有名为“numpy.core._multiarray_umath”的模块

Cha*_*ate 13 linux numpy amazon-web-services pandas aws-lambda

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

\n

我正在使用以下依赖项:

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

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

\n
[ERROR] ImportError: Unable to import required dependencies:\nnumpy: \n\nIMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!\n\nImporting the numpy c-extensions failed.\n- Try uninstalling and reinstalling numpy.\n- If you have already done that, then:\n  1. Check that you expected to use Python3.8 from "/var/lang/bin/python3.8",\n     and that you have no directories in your PATH or PYTHONPATH that can\n     interfere with the Python and numpy version "1.18.2" you're trying to use.\n  2. If (1) looks fine, you can open a new issue at\n     https://github.com/numpy/numpy/issues.  Please include details on:\n     - how you installed Python\n     - how you installed numpy\n     - your operating system\n     - whether or not you have multiple versions of Python installed\n     - if you built from source, your compiler versions and ideally a build log\n\n- If you're working with a numpy git repository, try `git clean -xdf`\n  (removes all files not under version control) and rebuild numpy.\n\nNote: this error has many possible causes, so please don't comment on\nan existing issue about this - open a new one instead.\n\nOriginal error was: No module named 'numpy.core._multiarray_umath'\n\nTraceback (most recent call last):\n\xc2\xa0\xc2\xa0File "/var/task/src/py38-lib-test.py", line 28, in py38test\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0import pandas\n\xc2\xa0\xc2\xa0File "/tmp/lib/pandas/__init__.py", line 16, in <module>\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0raise ImportError(END RequestId: 07762380-1fc4)\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

预先感谢您的评论。

\n

Bri*_*ian 5

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

假设您在区域中运行 python 3.8 us-east-1,根据此 Klayers 文档,您可以将arn:aws:lambda:us-east-1:770693421928:layer:Klayers-p38-numpy:9其用作您的层,以便可以import numpy在 lambda 函数中运行。


Mar*_*ick 2

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

类似于 docker lambda 的东西:

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