# No module named 'pydantic\_core.\_pydantic\_core'

**URL:** <https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616>\
**Category:** Serverless Framework\
**Tags:** aws, lambda\
**Created:** [October 24, 2023, 2:13am UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616 "2023-10-24T02:13:23Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![boompig](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/boompig/32/7694_2.png) [@boompig](https://forum.serverless.com/u/boompig)\
**Post date:** [October 24, 2023, 2:13am UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/1 "2023-10-24T02:13:23Z")

</div>

I am trying to deploy a python lambda with the serverless framework which uses the pydantic module (among other things). I am also using the serverless-python-requirements plugin. I am getting this error:

Running “serverless” from node\_modules  
{  
“errorMessage”: “No module named ‘pydantic\_core.\_pydantic\_core’”,  
“errorType”: “ModuleNotFoundError”,  
“requestId”: “44cde922-0d35-406b-97be-2d02a4e1fc8d”,  
“stackTrace”: [  
" File "/opt/python/serverless\_aws\_lambda\_sdk/instrument/ **init**.py", line 598, in stub\n return self.\_handler(user\_handler, event, context)\n",  
" File "/opt/python/serverless\_aws\_lambda\_sdk/instrument/ **init**.py", line 580, in \_handler\n result = user\_handler(event, context)\n",  
" File "/var/task/serverless\_sdk/ **init**.py", line 144, in wrapped\_handler\n return user\_handler(event, context)\n",  
" File "/var/task/s\_post\_rental\_app\_from\_airtable.py", line 25, in error\_handler\n raise e\n",  
" File "/var/task/s\_post\_rental\_app\_from\_airtable.py", line 20, in \n user\_handler = serverless\_sdk.get\_user\_handler(‘functions.post\_rental\_app\_from\_airtable.handler.lambda\_handler’)\n",  
" File "/var/task/serverless\_sdk/ **init**.py", line 56, in get\_user\_handler\n user\_module = import\_module(user\_module\_name)\n",  
" File "/var/lang/lib/python3.10/importlib/ **init**.py", line 126, in import\_module\n return \_bootstrap.\_gcd\_import(name[level:], package, level)\n",  
" File "", line 1050, in \_gcd\_import\n",  
" File "", line 1027, in \_find\_and\_load\n",  
" File "", line 1006, in \_find\_and\_load\_unlocked\n",  
" File "", line 688, in \_load\_unlocked\n",  
" File "", line 883, in exec\_module\n",  
" File "", line 241, in \_call\_with\_frames\_removed\n",  
" File "/var/task/functions/post\_rental\_app\_from\_airtable/handler.py", line 9, in \n from pydantic import BaseModel, ValidationError\n",  
" File "/var/task/pydantic/ **init**.py", line 3, in \n import pydantic\_core\n",  
" File "/var/task/pydantic\_core/ **init**.py", line 6, in \n from .\_pydantic\_core import (\n"  
]  
}

This is my serverless.yml:  
[skipping header]

provider:  
name: aws

# latest supported python by lambda + serverless as of 2023-10-23

runtime: python3.10

# NOTE: arm64 may offer better price/performance

architecture: ‘arm64’  
stackTags:  
Project: reffie

functions:  
post\_rental\_app\_from\_airtable:  
handler: functions.post\_rental\_app\_from\_airtable.handler.lambda\_handler  
events:  
- http:  
path: /manual-trigger  
method: post

plugins:

- serverless-python-requirements

package:  
patterns:  
# virtualenv  
- ‘!env310/**’  
# unit tests and their data  
- '!tests/**’  
# local node\_modules  
- ‘!node\_modules/**’  
# python cache  
- '!pycache/**’  
- ‘!\*\*/\*.pyc’  
# Mac files  
- ‘!.DS\_Store’

And my requirements.txt:

annotated-types==0.6.0  
certifi==2023.7.22  
charset-normalizer==3.3.1  
greenlet==3.0.0  
idna==3.4  
inflection==0.5.1  
pyairtable==2.1.0.post1  
pydantic==2.4.2  
pydantic\_core==2.10.1  
python-dotenv==1.0.0  
requests==2.31.0  
SQLAlchemy==2.0.22  
typing\_extensions==4.8.0  
urllib3==2.0.7

I tried both with architecture as x86\_64 and as arm64. I read online that the error sometimes happens due to a mismatch between the architecture of the lambda and the architecture of the docker container used to package the python modules. Any indication where to start?

---

<div class="post-metadata">

**Author:** ![boompig](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/boompig/32/7694_2.png) [@boompig](https://forum.serverless.com/u/boompig)\
**Post date:** [October 24, 2023, 1:53pm UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/2 "2023-10-24T13:53:30Z")

</div>

I tried adding a custom serverless configuration to go with my lambda:

custom:  
pythonRequirements:  
# this is necessary to avoid cross-platform build issues  
dockerizePip: true  
# explicitly pass the arm64 platform to the docker build  
dockerImage: public.ecr.aws/sam/build-python3.10:latest-arm64  
# explicitly tell pip to fetch the arm64 version of the package  
dockerRunCmdExtraArgs: [‘–platform’, ‘linux/arm64/v8’]

and specified the lambda to use the arm64 architecture. Didn’t solve the issue.

---

<div class="post-metadata">

**Author:** ![jonrobinson](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jonrobinson/32/7727_2.png) [@jonrobinson](https://forum.serverless.com/u/jonrobinson)\
**Post date:** [November 10, 2023, 10:06pm UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/3 "2023-11-10T22:06:48Z")

</div>

Did you figure this out? I’m running into the same issue.

---

<div class="post-metadata">

**Author:** ![ernesrocker](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/ernesrocker/32/7740_2.png) [@ernesrocker](https://forum.serverless.com/u/ernesrocker)\
**Post date:** [November 22, 2023, 2:19pm UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/4 "2023-11-22T14:19:49Z")

</div>

@boompig @jonrobinson I had the same error using these dependencies in the requirements.txt:

```auto
boto3==1.28.1
boto3-stubs==1.28.68
pydantic==2.4.0
pydantic[email]
pydantic-settings==2.1.0
pytest==7.3.1
pytest-mock==3.11.1
mock==5.0.2
pipreqs==0.4.13
black==23.7.0
backoff==2.2.1
mypy==1.5.1

```

But there aren’t any dependency issues; the errors occur when you build the dependecy package (in **pip install** step) for your Lambda in a different architecture . In my case, I use a devcontainer with VSCode with a Docker image that I build:

1. My first mistake was this:

```auto
FROM ubuntu:latest

```

Basically, I didn’t specify the OS architecture in my Dockerfile. So, I replaced that line:

```auto
FROM arm64v8/ubuntu:latest

```

1. Second mistake in serverless.yml:  
If you don’t specify the type of architecture that you want to use in your Lambdas, AWS sets the default to **x86\_64 – 64-bit x86 architecture for x86-based processors**. So, I added the default architecture type for all my Lambdas:

```auto
provider:
  name: aws
  runtime: python3.10
  architecture: arm64

```

I also specified the architecture type in the default layer definition:

```auto
 layer:
      name: ${self:custom.resources.layers.commons.name}
      description: Python requirements lambda layer
      compatibleRuntimes:
        - python3.10
      compatibleArchitectures: # optional, a list of architectures this layer is compatible with
        - arm64

```

Finally, I deployed my project again and solved my issue.

Conclusion: You must build your dependency package on the same architecture you use to deploy your Lambdas.

PD: About arm64 architecture vs x86 architecture: [Lambda instruction set architectures (ARM/x86) - AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/foundation-arch.html?icmpid=docs_lambda_help#foundation-arch-adv)

---

<div class="post-metadata">

**Author:** ![jonrobinson](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jonrobinson/32/7727_2.png) [@jonrobinson](https://forum.serverless.com/u/jonrobinson)\
**Post date:** [February 22, 2024, 6:43pm UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/5 "2024-02-22T18:43:13Z")

</div>

Sorry for the late reply. Thanks for the update!

---

<div class="post-metadata">

**Author:** ![adobelis](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/adobelis/32/7870_2.png) [@adobelis](https://forum.serverless.com/u/adobelis)\
**Post date:** [April 5, 2024, 3:59pm UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/6 "2024-04-05T15:59:08Z")

</div>

Downgrading to `pydantic_core==2.10.1` (and then unsetting dependent package versions to avoid conflicts – `pydantic` version becomes 1.9.0) resolved this issue for me. Other relevant configs I am using:

- Python version: 3.11
- Architecture: not specified. Defaults to `x86_64` as per Lambda config.

---

<div class="post-metadata">

**Author:** ![jota12x](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/jota12x/32/7885_2.png) [@jota12x](https://forum.serverless.com/u/jota12x)\
**Post date:** [April 30, 2024, 3:19pm UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/7 "2024-04-30T15:19:19Z")

</div>

I encountered the same error and solved it by combining the answers from @ernesrocker and @adobelis. Hope to find a better way soon…

---

<div class="post-metadata">

**Author:** ![valentinafeve](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/valentinafeve/32/8009_2.png) [@valentinafeve](https://forum.serverless.com/u/valentinafeve)\
**Post date:** [August 6, 2024, 2:43pm UTC](https://forum.serverless.com/t/no-module-named-pydantic-core-pydantic-core/19616/8 "2024-08-06T14:43:52Z")

</div>

This config worked for me:

```yaml
provider:
  name: aws
  runtime: python3.11
  architecture: arm64

```

requirements.txt

```auto
pydantic==1.9.0
pydantic_core==2.10.1

```
