0

I am trying to import pydantic to a lambda via a lambda layer

and I keep getting this error

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

The lambda uses python 3.9,Architecture x86_64, I have tried these commands separately :

  • docker run -i -v $(pwd)/layer:/app -w /app --entrypoint /bin/bash public.ecr.aws/lambda/python:3.9 -c "pip3 install --target ./python -r requirements.txt"

  • pip3 install --target layer/python -r requirements.txt --upgrade

  • docker run -i -v $(pwd)/layer:/app -w /app --user $(id -u):$(id -g) --entrypoint /bin/bash python:3.10.13-slim-bullseye -c "pip3 install --target ./python -r requirements.txt --upgrade"

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

and then compress the packages zip -r layer.zip layer/python and upload the layer and import it in the lambda

and I keep getting this error

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

4
  • python -m pip install pydantic Commented Jan 31, 2024 at 23:05
  • Are there any pydantic in requirements.txt? Commented Feb 1, 2024 at 7:51
  • Please provide the requirements.txt and the code snippet where you perform these imports. Commented Feb 1, 2024 at 8:06
  • using this fixed the issue with updating the lambda python version to 3.9 pip install -r requirements.txt --python-version 3.9 --platform manylinux2014_x86_64 --target layer/python --only-binary=:all: as well as using a zip file that includes only python directory instead of layer/python Commented Feb 3, 2024 at 1:08

1 Answer 1

1

The following fixed the issue

  1. using pip install -r requirements.txt --python-version 3.9 --platform manylinux2014_x86_64 --target layer/python --only-binary=:all:
  2. make sure that the lambda python version is 3.9
  3. when creating the zip file it should be python/pydantic so it can be imported from the layer correctly
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.