Skip to content

How do I install a Lambda compatible Python deployment package with compiled code on macOS or Windows OS?

3 minute read
1

I used pip to install a Python package that contains compiled code, and now my AWS Lambda function returns an "Unable to import module" error.

Short description

Python packages that contain compiled code, such as NumPy and pandas might not be compatible with Lambda runtimes. If you use pip to install these python packages, then the packages download and compile a module-name package for the architecture of the local machine. If you don't use a Linux operating system (OS), then this makes your deployment package incompatible with Lambda Python runtimes.

To make your deployment package or layer compatible with Lambda on a macOS or Windows OS, run the pip install command. When you run the command, use manylinux2014 as the value for the --platform parameter.

Note: macOS --platform tags aren't compatible with Lambda deployment package installs. For example, the win_amd64 and macosx_10_6_intel tags don't install a deployment package that's compatible with Lambda.

Resolution

The following steps are examples of how to install pandas for the Lambda Python 3.13 runtime that runs on x86_64 architecture.

  1. Open a command prompt and run the following pip command to confirm that your version of pip is version 19.3.0 or newer:

    pip --version
  2. (Optional) If your version of pip is older than pip version 19.3.0, then run the following command to upgrade to the latest version of pip:

    python3.13 -m pip install --upgrade pip 
  3. Install the precompiled Python package's .whl file as a dependency in your Lambda function's project directory:

    pip install \
        --platform manylinux2014_x86_64 \
        --target=your-lambda-function \
        --implementation cp \
        --python-version 3.13 \
        --only-binary=:all: --upgrade \
        pandas

    Note: Replace your-lambda-function with the name of your function's project directory.

  4. Open your Lambda function's project directory. If you use macOS, then run the following command to open the project directory:

    cd your-lambda-function
  5. In a text editor, create a new file named lambda_function.py. Then, copy and paste the following example code into the file:

    import numpy as np
    import pandas as pd
    def lambda_handler(event, context):
        df2 = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),columns=["a", "b", "c"])
        number = np.pi
        print(df2)
        print(number)
  6. Save this file in your Lambda function's project directory.

  7. Create a Lambda deployment package .zip file archive that includes all the installed libraries and source code:

    zip -r ../my-deployment-package.zip .
  8. Use the my-deployment-package.zip file archive to either create a new Python 3.13 Lambda function or to update an existing one. For instructions, see Working with .zip file archives for Python Lambda functions.

  9. (Optional) Use a similar procedure to create a Lambda layer used across multiple functions. For example, the following command creates a new Lambda layer to install pandas for the Lambda Python 3.13 runtime, that runs on arm64 architecture:

    pip install \
        --platform manylinux2014_aarch64 \
        --target=./python/lib/python3.13/site-packages \
        --implementation cp \
        --python-version 3.13 \
        --only-binary=:all: --upgrade \
        pandas

Related information

Managing Lambda dependencies with layers

Troubleshoot deployment issues in Lambda

How do I resolve the "Unable to import module" error that I receive when I run Lambda code in Python?

AWS OFFICIALUpdated 4 months ago
10 Comments

Hey there,

Thanks for the guide! One issue that I noticed is that --python 3.9 should be --python-version 3.9. Otherwise, the dependencies will be installed for whatever version of Python is running on the host.

I am deploying a lambda layer via a CodeBuild project, and this seemed to work for me.

replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
MODERATOR

replied 3 years ago

I have tried those steps mentioned above, but I keep getting the same error. I am doing that through an AWS CDK app, that I am deploying through a CodePipeline.

Error:

{ "errorMessage": "Unable to import module 'pandasLambda': 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. This error can happen for\nmany reasons, often due to issues with your setup or how NumPy was\ninstalled.\n\nWe have compiled some common reasons and troubleshooting tips at:\n\n https://numpy.org/devdocs/user/troubleshooting-importerror.html\n\nPlease note and check the following:\n\n * The Python version is: Python3.9 from "/var/lang/bin/python3.9"\n * The NumPy version is: "1.24.3"\n\nand make sure that they are the versions you expect.\nPlease carefully study the documentation linked above for further help.\n\nOriginal error was: No module named 'numpy.core._multiarray_umath'\n", "errorType": "Runtime.ImportModuleError", "requestId": "", "stackTrace": [] }

replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
EXPERT

replied 3 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
EXPERT

replied 3 years ago

this work for 3.10 runtime and mayors versions ?

replied 2 years ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

AWS
MODERATOR

replied 2 years ago

It appears that AL2023 x86_64 is not available with Python-3.12, the latest version available on DNF is 3.11, so the Python lambda layer must be built with 3.11, and the Lambda runtime also set to 3.11

replied 2 years ago

This article was reviewed and updated on 2026-05-06.

AWS
EXPERT

replied 4 months ago