Inacio da Motta
Inacio da Motta

Reputation: 1

No module named 'pydantic_core._pydantic_core' using FastAPI on Azure Functions

I am deploying and Azure function using FastAPI using Azure Pipelines. I am getting an error stating the module pydantic_core is missing but I can see in my build library it is present. I have tried clearing the functionapp cache, playing around with different version of python (3.9,3.10 and 3.11) to no avail.

I am new to Azure.

Searching online I have found similar errors with AWS but no one of the posted solutions seems to work for me.

The function works fine running locally outside of Azure.

Here is the error dump from the function invocation.

Result: Failure Exception: ModuleNotFoundError: No module named 'pydantic_core._pydantic_core'. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound Stack: File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 387, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 48, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 44, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/loader.py", line 194, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/HttpFunctionHandler/init.py", line 1, in from fastapi import FastAPI, Request File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/init.py", line 7, in from .applications import FastAPI as FastAPI File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/applications.py", line 16, in from fastapi import routing File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/routing.py", line 22, in from fastapi import params File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/params.py", line 5, in from fastapi.openapi.models import Example File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/openapi/models.py", line 4, in from fastapi._compat import ( File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/_compat.py", line 20, in from fastapi.exceptions import RequestErrorModel File "/home/site/wwwroot/.python_packages/lib/site-packages/fastapi/exceptions.py", line 3, in from pydantic import BaseModel, create_model File "/home/site/wwwroot/.python_packages/lib/site-packages/pydantic/init.py", line 372, in getattr module = import_module(module_name, package=package) File "/usr/local/lib/python3.10/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/.python_packages/lib/site-packages/pydantic/main.py", line 11, in import pydantic_core File "/home/site/wwwroot/.python_packages/lib/site-packages/pydantic_core/init.py", line 6, in from ._pydantic_core import (

I have tried multiple versions of python (3.9, 3.10 and 3.11)

Different versions of FastAPI (now on fastapi==0.104.1)

Stop functionapp, Clear functionapp cache and restart function app in the pipeline yaml

Upvotes: 0

Views: 1114

Answers (1)

SiddheshDesai
SiddheshDesai

Reputation: 8187

According to this Github comment adding fastapi==0.99.1 pydantic==1.10.12 version in requirements.txt has resolve the pydantic import error.

I have referred this Fast API sample and the deployment of the Function was successful.

Added below packages in my requirements.txt:-

azure-functions>=1.12.0
fastapi
pydantic==1.10.12 

My Azure DevOps YAML code:-

trigger:
- main

variables:
  
  azureSubscription: 'xxxxxxx76ed'

  
  functionAppName: 'valley-func87'

  
  vmImageName: 'ubuntu-latest'

 
  workingDirectory: '$(System.DefaultWorkingDirectory)'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - bash: |
        if [ -f extensions.csproj ]
        then
            dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
        fi
      workingDirectory: $(workingDirectory)
      displayName: 'Build extensions'

    - task: UsePythonVersion@0
      displayName: 'Use Python 3.10'
      inputs:
        versionSpec: 3.10 # Functions V2 supports Python 3.6 as of today

    - bash: |
        pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
      workingDirectory: $(workingDirectory)
      displayName: 'Install application dependencies'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(workingDirectory)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'development'
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionAppLinux
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

I have created Azure Function App with the below properties:-

enter image description here

Output:-

enter image description here

enter image description here

enter image description here

Upvotes: 0

Related Questions