Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No module named 'pydantic_core._pydantic_core' in AWS Lambda though library is installed for FastAPI based code

AWS lambda deployment of FastAPI gives the following error:

[ERROR] Runtime.ImportModuleError: Unable to import module 'users_crud': No module named 'pydantic_core._pydantic_core'
Traceback (most recent call last):

Though the pydantic lib is already installed. I am using version 3.10 which is now supported by AWS.

like image 983
Suhail Abdul Rehman Chougule Avatar asked Sep 13 '25 15:09

Suhail Abdul Rehman Chougule


1 Answers

Lambda requires packages built for a specific architecture. Many packages have distributions for multiple architectures (see available distributions for pydantic-core). By default, pip installs the distribution suitable for the machine where you are running it on, which is not necessary the same architecture as your Lambda.

But you can force pip to install packages for the architecture you want. If your Lambda uses x86_64, then you should select platform manylinux2014_x86_64:

pip install pydantic-core --platform manylinux2014_x86_64 -t . --only-binary=:all:

-t . - means install in the current directory.

However, the best way is to install all your dependencies with the required platform, rather than doing it for each package.

pip install -r requirements.txt --platform manylinux2014_x86_64 --target ./python --only-binary=:all:

Credits to this answer.

like image 167
Alena Melnikova Avatar answered Sep 15 '25 04:09

Alena Melnikova