Error: class uri 'uvicorn.workers.UvicornWorker' invalid or not found

Anon 20 Reputation points
2024-05-25T17:05:18.5566667+00:00

Hello,

I set up a CICD pipeline to handle deploying python code to the App Service but get this screen

User's image

In the console it says "Failed to load resource: the server responded with a status of 503 (Service Unavailable)"

When I look in the logs, the main error says "ModuleNotFoundError: No module named 'uvicorn'". According to the docs this type of error happens when a virtual environment is also deployed. I did that at first but removed the line to create a virtual environment in my pipeline file (shown below) but the error still persists, I made a fresh app running the pipeline without creating a new virtual environment and the error still persists.

My app settings has SCM_DO_BUILD_DURING_DEPLOYMENT set to 1 as mentioned on the docs but it is still not working

User's image

My wwwroot folder

User's image

The logs

2024-05-25T15:32:29.530049885Z    _____                               
2024-05-25T15:32:29.530085985Z   /  _  \ __________ _________   ____  
2024-05-25T15:32:29.530108485Z  /  /_\  \\___   /  |  \_  __ \_/ __ \ 
2024-05-25T15:32:29.530112385Z /    |    \/    /|  |  /|  | \/\  ___/ 
2024-05-25T15:32:29.530115585Z \____|__  /_____ \____/ |__|    \___  >
2024-05-25T15:32:29.530119085Z         \/      \/                  \/ 
2024-05-25T15:32:29.530122285Z A P P   S E R V I C E   O N   L I N U X
2024-05-25T15:32:29.530125385Z 
2024-05-25T15:32:29.530128285Z Documentation: http://aka.ms/webapp-linux
2024-05-25T15:32:29.530147085Z Python 3.12.2
2024-05-25T15:32:29.530150085Z Note: Any data outside '/home' is not persisted
2024-05-25T15:32:31.826079978Z Starting OpenBSD Secure Shell server: sshd.
2024-05-25T15:32:31.982245355Z Site's appCommandLine: gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:api
2024-05-25T15:32:31.990065699Z Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -userStartupCommand 'gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:api'
2024-05-25T15:32:32.098018323Z Could not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
2024-05-25T15:32:32.098059122Z Could not find operation ID in manifest. Generating an operation id...
2024-05-25T15:32:32.105534069Z Build Operation ID: 9409a21f-8457-436a-baca-bfe4afa14977
2024-05-25T15:32:32.592525967Z Oryx Version: 0.2.20240319.4, Commit: 350bad7f37fa55a2d965474e7e95f9075e989e10, ReleaseTagName: 20240319.4
2024-05-25T15:32:32.600564210Z Writing output script to '/opt/startup/startup.sh'
2024-05-25T15:32:32.741688495Z WARNING: Could not find virtual environment directory /home/site/wwwroot/antenv.
2024-05-25T15:32:32.741749595Z WARNING: Could not find package directory /home/site/wwwroot/__oryx_packages__.
2024-05-25T15:32:34.363265237Z 
2024-05-25T15:32:34.363307036Z Error: class uri 'uvicorn.workers.UvicornWorker' invalid or not found: 
2024-05-25T15:32:34.363336536Z 
2024-05-25T15:32:34.363341136Z [Traceback (most recent call last):
2024-05-25T15:32:34.363344436Z   File "/opt/python/3.12.2/lib/python3.12/site-packages/gunicorn/util.py", line 111, in load_class
2024-05-25T15:32:34.363347936Z     mod = importlib.import_module('.'.join(components))
2024-05-25T15:32:34.363351236Z           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-25T15:32:34.363354636Z   File "/opt/python/3.12.2/lib/python3.12/importlib/__init__.py", line 90, in import_module
2024-05-25T15:32:34.363374136Z     return _bootstrap._gcd_import(name[level:], package, level)
2024-05-25T15:32:34.363377136Z            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-05-25T15:32:34.363380236Z   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
2024-05-25T15:32:34.363395836Z   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
2024-05-25T15:32:34.363399736Z   File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
2024-05-25T15:32:34.363402836Z   File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
2024-05-25T15:32:34.363405836Z   File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
2024-05-25T15:32:34.363408836Z   File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
2024-05-25T15:32:34.363411936Z   File "<frozen importlib._bootstrap>", line 1324, in _find_and_load_unlocked
2024-05-25T15:32:34.363414935Z ModuleNotFoundError: No module named 'uvicorn'
2024-05-25T15:32:34.363417935Z ]
2024-05-25T15:32:34.370727983Z  


The pipeline code:

stages:
  - stage: Build
    displayName: Build Stage
    jobs:
    - job: BuildJob
      steps:
      - task: UsePythonVersion@0
        inputs:
          versionSpec: '$(pythonVersion)' #using python 3.12
        displayName: 'Use Python $(pythonVersion)'

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

      - publish: $(Build.ArchifactStagingDirectory)/$(Build.BuildId).zip
        displayName: 'Upload Package'
        artifact: drop

  - stage: Deploy
    displayName: 'Deploy Web App'
    dependsOn: Build
    condition: succeeded()
    jobs:
    - deployment: DeploymentJob
      pool:
        vmImage: $(vmImageName)
      environment: $(environmentName)
      strategy:
        runOnce:
          deploy:
            steps:
              - task: UsePythonVersion@0
                inputs:
                  versionSpec: '$(pythonVersion)'
                displayName: 'Use Python $(pythonVersion)'

              - task: AzureWebApp@1
                displayName: 'Deploy to Azure Web App'
                inputs:
                  appType: webapp
                  azureSubscription: $(azureServiceConnectionId)
                  appName: $(webAppName)
                  package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

Even when I try to install uvicorn directly through the startup command it still fails. I'm using FastAPI, uvicorn, and gunicorn on linux machine. Please advise.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,212 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. brtrach-MSFT 15,696 Reputation points Microsoft Employee
    2024-05-30T05:24:21.8+00:00

    @Anon It seems like the error is caused by the fact that the uvicorn module is not installed on the App Service. You can try installing it by adding a requirements.txt file to your project root directory with the following content:

    uvicorn
    

    Then, add a step to your pipeline to install the dependencies before deploying the app:

    -
    

    This should install the uvicorn module and its dependencies before deploying the app.

    Remember to commit the changes to your requirements.txt file and push them to your repository before deploying your app. This ensures that the Azure App Service pulls the latest version of your requirements.txt file when deploying your app.

    Also, please note that the versions of the packages can also be specified in the requirements.txt file. For example, if you want to install uvicorn version 0.15.0, you would specify it like this: uvicorn==0.15.0. If no version is specified, pip will install the latest version available.


  2. Anon 20 Reputation points
    2024-05-31T15:43:42.91+00:00

    Eventually solved this by updating the pipeline to include a part to create a virtual environment and download the modules I need. I downloaded uvicorn directly in the script and created another bash file full of commands to download each module I need as for some reason requirements.txt wasn't working. Pipeline:

    stages:
      - stage: Build
        displayName: Build Stage
        jobs:
        - job: BuildJob
          pool:
            vmImage: $(vmImageName)
          steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
            displayName: 'Use Python $(pythonVersion)'
            
          - script: |
              python -m venv antenv
              source antenv/bin/activate
              python -m pip install --upgrade pip
              pip install setup
              pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt
              echo "Installing Requirements"
              sh requirements.sh
              pip install fastapi uvicorn
              echo "The uvicorn version installed is:"
              uvicorn --version
            workingDirectory: $(projectRoot)
            displayName: "Install requirements"
    
          - task: ArchiveFiles@2
            displayName: 'Archive files'
            inputs:
              rootFolderOrFile: '$(projectRoot)'
              includeRootFolder: false
              archiveType: zip
              archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
              replaceExistingArchive: true
    
          - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
            displayName: 'Upload package'
            artifact: drop
    
      - stage: Deploy
        displayName: 'Deploy Web App'
        dependsOn: Build
        condition: succeeded()
        jobs:
        - deployment: DeploymentJob
          pool:
            vmImage: $(vmImageName)
          environment: $(environmentName)
          strategy:
            runOnce:
              deploy:
                steps:
                  - task: UsePythonVersion@0
                    inputs:
                      versionSpec: '$(pythonVersion)'
                    displayName: 'Use Python $(pythonVersion)'
    
                  - task: AzureWebApp@1
                    displayName: 'Deploy Azure Web App : $(webAppName)'
                    inputs:
                      azureSubscription: $(azureServiceConnectionId)
                      appName: $(webAppName)
                      package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
                      startUpCommand: 'gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:api'
                      appType: 'webAppLinux'
    
    0 comments No comments