5

I'm working in a project develop with serverless framework, Flask and MongoDB. The problem it's when I try to install pydantic, in the traceback say's ModuleNotFoundError: No module named 'pydantic_core._pydantic_core' but in my requirements is specify the library.

Is there a form to install pydantic or is not possible install the library in Lambda's AWS?

Traceback in Cloudwatch

File "/var/task/app.py", line 5, in <module>
from pydantic import ValidationError
File "/var/task/pydantic/__init__.py", line 3, in <module>
import pydantic_core
File "/var/task/pydantic_core/__init__.py", line 6, in <module>
from ._pydantic_core import (
ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'
[ERROR] Exception: Unable to import app.app
Traceback (most recent call last):
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/task/wsgi_handler.py", line 115, in <module>
    wsgi_app = import_app(config)
  File "/var/task/wsgi_handler.py", line 48, in import_app
    raise Exception("Unable to import 
{}
".format(config["app"]))

I've tried downgrading the version of python, pydantic, Flask. I also specified the library pydantic_core and not working.

requirements.txt

Flask==1.1.4
Werkzeug==1.0.1
markupsafe==2.0.1
pymongo==4.5.0
pydantic==2.2.1
pydantic_core==2.6.1
email-validator==2.0.0.post2

serverless.yml

service: users

frameworkVersion: "3"

custom:
  wsgi:
    app: app.app

provider:
  name: aws
  runtime: python3.11
  stage: dev
  region: us-west-2
  environment:
    MONGO_URI: <mongo_uri>

functions:
  api:
    handler: wsgi_handler.handler
    events:
      - httpApi: "*"

plugins:
  - serverless-wsgi
  - serverless-python-requirements
2
  • 1
    You must include dependencies with your code. This should help: docs.aws.amazon.com/lambda/latest/dg/… Commented Aug 23, 2023 at 15:21
  • 1
    I don't think this is enough. It seems that pydantic_core library is architecture-dependent, and its not working correctly on the lambdas deployed by serverless framework. Commented Sep 1, 2023 at 22:18

1 Answer 1

2

I suggest using serverless-python-requirements plugin and configure it as follow which will resolve your issue, but note that you need to have docker installed and running in order for layer to get built.

pythonRequirements:
    dockerizePip: non-linux
    dockerRunCmdExtraArgs: ["--platform", "linux/x86_64"]
    usePipenv: false
    slim: true
    layer: true

I have answered this in detail in the following article, Hope this helps.

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.