Skip to content

Unable to import numpy in AWS Lambda function #13465

Closed

Description

@ljames1
  • how you installed Python
    deleted python3 on my mac and then brew install python to install python 3.7.3
  • how you installed numpy
    Since I am installing to run on AWS Lambda, pip install numpy --target .
  • your operating system
    macOS Sierra 10.12.6
  • whether or not you have multiple versions of Python installed
    the native python 2.7.10 is still installed on my mac
  • if you built from source, your compiler versions and ideally a build log
    N/A

So I am able to run my code locally and I have no issues. I am pandas, matplotlib, boto3, and mpld3 to organize and display data in an AWS DDB table with matplotlib graphs that mpld3 turns into html. numpy appears to be needed for pandas, and whenever I try to install these libraries to a target directory so they can run in a lambda function, according to https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html, I get the following error:


Importing the multiarray numpy extension module failed.  Most
likely you are trying to import a failed build of numpy.
Here is how to proceed:
- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.
- If you are simply trying to use the numpy version that you have installed:
  your installation is broken - please reinstall numpy.
- If you have already reinstalled and that did not fix the problem, then:
  1. Check that you are using the Python you expect (you're using /var/lang/bin/python3.6),
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy versions 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

     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'


END RequestId: 2fc65f50-420d-441b-930c-665b1c8ab3ea
REPORT RequestId: 2fc65f50-420d-441b-930c-665b1c8ab3ea	Duration: 0.85 ms	Billed Duration: 100 ms 	Memory Size: 128 MB	Max Memory Used: 40 MB	```
  

Activity

  1. ljames1 commented on May 4, 2019

    @ljames1
    Author

    To isolate the issue, I retried the above with:

    def main(event, context):
    
        a = 100
        print(a)
        return
    

    and

    import numpy as np
    
    def main(event, context):
    
        a = np.array(100)
        print(a)
        return
    

    Both files were zipped a package directory containing a targeted installation of numpy according to https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html. After zipping, the files were ~16MB, so they had the numpy installation. The first example that did not import numpy runs fine when testing the Lambda function, but the second errors with the original error message

  2. changed the title [-]Numpy breaking when running pandas in lambda. Original error was: No module named 'numpy.core._multiarray_umath'[/-] [+]Unable to import numpy in AWS Lambda function[/+] on May 4, 2019
  3. rgommers commented on May 4, 2019

    @rgommers
    Member
  4. ljames1 commented on May 6, 2019

    @ljames1
    Author
  5. iceback commented on Oct 22, 2019

    @iceback

    @rgommers Do you know of any python3.7 related tacks on this. I've tried a couple from your list of 3.6 and always end up with the same gripe about the _multiarray lib. I need to make an AWS "layer" since the combination I need (numpy,scipy,pandas) exceeds size allowance of a single function.

  6. leehagoodjames commented on Oct 22, 2019

    @leehagoodjames

    @iceback if your error was similar to what I had, which was No module named 'numpy.core._multiarray_umath', this is caused by not installing numpy with the correct OS.

    AWS lambda is going to run AWS linux at runtime, so any target-installed packages should be installed with the proper OS. If you target install numpy to a package directory via pip install --target ./package numpy, and you are running another OS (such as macOS), then this will fail because the target installation assumes that the OS will not change from your native OS.

    To get around this, you have two options:

    1. Perform the target installation from a machine running AWS Linux, such as an EC2 instance. This worked great for me.
    2. Figure out how to specify the runtime OS in your target install command. If you find a good way to do this, please share 😄
  7. iceback commented on Oct 22, 2019

    @iceback

    I'll have to revisit all the things I tried last week which included the @korniichuk and github/pbegle and lord knows what else but always ended up in same spot. Building numpy on an EC2 is not going well. Perhaps wrong Cython version (only 0.27 available on aws linux). Can you elaborate on your #1. How do you get from an installed numpy on one instance to a layer on Lambda?

  8. leehagoodjames commented on Oct 22, 2019

    @leehagoodjames

    @iceback Ya this was frustrating when I first did it, but it is definitely possible. I currently run Lambda functions with Python 3.7 with the 3 packages you mentioned.

    How I performed step 1. above involved following the steps outlined in Updating a Function with Additional Dependencies, described here. For simplicity sake, I am going to assume that your local python file is named pony.py and the lambda_function is invoked with the ride_pony function.

    1. Launch and connect to an EC2 instance, such as an EC2 micro instance. Find instructions here. Ensure that the instance runs AWS Linux.
    2. Check that python3 is installed. If it is not installed, install it with sudo yum install python3 -y
    3. Make a directory for your local packages. mkdir package
    4. Install the libraries you want to use in Lambda. FYI I always call pip as a module to avoid confusion between Python2 and Python3 pips. Also, some installation-order may be preferred in these installations and that may be worth researching.
    • python3 -m pip install --target ./package numpy
    • python3 -m pip install --target ./package pandas
    • python3 -m pip install --target ./package scipy
    1. Move into installation directory with cd package
    2. Zip the installation directory with zip -r9 ../pony.zip .
    3. Now, from your local machine, scp pony.zip from your EC2 instance to your local with the directions here.
    4. From your local, zip pony.py into pony.zip. zip -g pony.zip pony.py. Your zip file now contains your lambda code along with the python modules needed, which were installed to target with an AWS Linux OS.
    5. Upload the zip file to S3 (which allows for larger lambda functions, up to 50 MB). If your code is small, you can skip to step 10.
    6. Go into your lambda function (or update from the CLI) to use the code in pony.zip for that lambda function

    **Ensure that your lambda function points to & executes your filename and main function, pony.ride_pony, if it is not the default lambda_function.lambda_handler

  9. iceback commented on Oct 22, 2019

    @iceback

    Thank you very much. I believe I have a shot! (My understanding is that the top of the zip has to be "python" for a Lambda layer so I'll use that instead of "package")

  10. iceback commented on Oct 22, 2019

    @iceback

    Shout that how-to out loud and proud! Not sure where I went afoul of the other suggestion (though they were for python3.6) but I'm now back to working on my function code. Thanks a ton.

  11. rgommers commented on Oct 23, 2019

    @rgommers
    Member

    AWS now also published a layer that includes NumPy and SciPy. From https://aws.amazon.com/blogs/aws/new-for-aws-lambda-use-any-programming-language-and-share-common-components/

    Based on our customer feedback, and to provide an example of how to use Lambda Layers, we are publishing a public layer which includes NumPy and SciPy, two popular scientific libraries for Python. This prebuilt and optimized layer can help you start very quickly with data processing and machine learning applications.

  12. iceback commented on Oct 23, 2019

    @iceback
  13. ajbenz18 commented on Jul 17, 2020

    @ajbenz18

    What worked for me was using a Linux version of the numpy library (I use macOS). I went to https://pypi.org/project/numpy/#files and downloaded the .whl file in the version i was looking for (for me, it was numpy-1.19.0-cp37-cp37m-manylinux1_x86_64.whl). Next go to the terminal and unzip it by doing 'unzip numpy-1.19.0-cp37-cp37m-manylinux1_x86_64.whl'. This should give you the numpy version that will work on Lambda. Then, zip everything up as you were doing before and upload it. In the end an incredibly frustrating problem was solved pretty simply.

  14. kp101994 commented on Aug 12, 2020

    @kp101994

    I had same problem, the solution that worked for me is that i uninstalled numpy from my pc,(windows 7).
    than i added layer in aws lambda function something called AWSlambda scipy and thats it.

  15. tuomastik commented on Nov 4, 2020

    @tuomastik

    I ran into the same issue with Windows but was able to solve it by either of the following approaches:

    • Build the Lambda with AWS SAM (Serverless Application Model) running on Ubuntu that runs on WSL (Windows Subsystem for Linux)
    • Build the Lambda with AWS SAM using the --use-container flag, which has the following explanation in the docs:

      If your functions depend on packages that have natively compiled dependencies, use this flag to build your function inside an AWS Lambda-like Docker container.

  16. DylanAlbertazzi commented on Apr 29, 2021

    @DylanAlbertazzi

    AWS provides a layer with Numpy and Scipy. Inside of the lambda console just click Add Layer then AWS Layers and it will come up at the top.

  17. ahulist commented on Jun 21, 2021

    @ahulist

    I'm having the same issue with AWS Data Wrangler + Numpy/Scipy combination. I need Data Wrangler to read Excel files from S3. The problem here is probably the fact that Data Wrangler incorporates Pandas which in turn incorporates its own Numpy... which is incompatible with Numpy from AWS Numpy/Scipy official Layer.
    How do I get around that?

  18. rkochar commented on Dec 3, 2023

    @rkochar

    Use manylinux.

    Example: pip install --platform=manylinux_2_17_x86_64 --only-binary=:all: -r requirements.txt -t .

  19. Nephthys76 commented on Dec 12, 2023

    @Nephthys76

    Use manylinux.

    Example: pip install --platform=manylinux_2_17_x86_64 --only-binary=:all: -r requirements.txt -t .

    This solved the error for me. Thank you for replying to such an old thread with your solution!

  20. schottz commented on Jul 15, 2024

    @schottz

    For those who are in the same situation as me and @leehagoodjames:

    We can use docker to perform this installation of the package aimed to the lambda function

    FROM --platform=linux/amd64 python:3.11
    
    WORKDIR /requirements
    
    CMD pip install -r requirements.txt -t /opt/python/
    

    build the image and run it mapping a volume on you local computer:

    docker build -t lambda_layer .
    docker run --rm --platform linux/amd64 -v $(pwd)/src/layer/python/lib/python3.11/site-packages:/opt/python -v $(pwd):/requirements lambda_layer
    
  21. jbram22 commented on Jan 18, 2025

    @jbram22

    Hi All, for those still struggling, I found a solution inspired by the one suggested by the team at Pydantic:
    https://docs.pydantic.dev/latest/integrations/aws_lambda/#installing-pydantic-for-aws-lambda-functions

    For reference, I am using MacOS, creating a virtual environment, then pip installing yfinance to eventually use as a layer for my AWS Lambda function.

    Basically what you do is force a linux distribution when pip installing via the following command:

    pip install --platform manylinux2014_x86_64 --target=python/lib/python3.11/site-packages --implementation cp --python-version 3.11 --only-binary=:all: --upgrade yfinance

    Another useful tidbit, to tidy up your zipped file and reduce the overall space it takes, is to only include the lib directory in your virtual environment. I titled my venv python to match conventions provided by AWS.

    zip -r yfinance.zip python -x python/bin/\* python/include/\*

  22. toobs4 commented on Feb 21, 2025

    @toobs4

    Hi All, for those still struggling, I found a solution inspired by the one suggested by the team at Pydantic: https://docs.pydantic.dev/latest/integrations/aws_lambda/#installing-pydantic-for-aws-lambda-functions

    For reference, I am using MacOS, creating a virtual environment, then pip installing yfinance to eventually use as a layer for my AWS Lambda function.

    Basically what you do is force a linux distribution when pip installing via the following command:

    pip install --platform manylinux2014_x86_64 --target=python/lib/python3.11/site-packages --implementation cp --python-version 3.11 --only-binary=:all: --upgrade yfinance

    Another useful tidbit, to tidy up your zipped file and reduce the overall space it takes, is to only include the lib directory in your virtual environment. I titled my venv python to match conventions provided by AWS.

    zip -r yfinance.zip python -x python/bin/\* python/include/\*

    i'm also trying to create a lambda layer with yfinance. i also need pandas and numpy in that layer.
    but when i run the lambda, i get the you should not try to import numpy from its source directory error
    @jbram22 can you pls provide the exact way you did this?

  23. ei-grad commented on Jul 24, 2025

    @ei-grad

    Faced similar problem, in my case it was because python3.12 lambda runtime requires at least x86_64-manylinux_2_28 python platform (or maybe it was because uv failed to build numpy properly enough for aws lambda .zip archive, and there is only 2_28 wheels for fresh numpy on pypi.org?).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions