pythonazuredeploymentfastapicontinuous-deployment

ModuleNotFoundError: No module named 'database in Azure error log


ModuleNotFoundError: No module named 'database is the error i can see when looking into the web app logs in Azure.

Im sure my database.py is in the same dir as the main.py and when i run it locally it works. i run it locally with: uvicorn main:app --reload. This works.

when i deploy it, it succeeds in GIT, it says deployed. But when i check the web service azure LOG it says the no database found. for the startup command in azure i use: gunicorn -w 4 -k uvicorn.workers.UvicornWorker main:app

Picture of LOG

For context: im using a github repo as deployment center in azure(and im trying to deploy a fastapi backend)

Looked on stack overflow for someone who had a similar problem, looked on youtube.

Github repo: https://github.com/DeNaamIsDaaf/Backend-FastAPI/tree/main


Solution

  • Based on the log stream provided, the error might be related to the startup command in the configuration of the Azure App Service.

    I created a simple FastAPI application and successfully deployed it to Azure App Service without any issues.

    WorkflowFile :

    name: Build and deploy Python app to Azure Web App - Sfastapitest1
    on:
    push:
    branches:
    - main
    workflow_dispatch:
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - name: Set up Python version
    uses: actions/setup-python@v1
    with:
    python-version: '3.12'
    - name: Create and start virtual environment
    run: |
    python -m venv venv
    source venv/bin/activate
    - name: Install dependencies
    run: pip install -r requirements.txt
    - name: Zip artifact for deployment
    run: zip release.zip ./* -r
    - name: Upload artifact for deployment jobs
    uses: actions/upload-artifact@v3
    with:
    name: python-app
    path: |
    release.zip
    !venv/
    deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
    name: 'Production'
    url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    steps:
    - name: Download artifact from build job
    uses: actions/download-artifact@v
    with:
    name: python-app
    - name: Unzip artifact for deployment
    run: unzip release.zip
    - name: 'Deploy to Azure Web App'
    uses: azure/webapps-deploy@v2
    id: deploy-to-webapp
    with:
    app-name: 'Sfastapitest1'
    slot-name: 'Production'
    ```yaml
    publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_<SecretKey>}}
    

    It was successfully deployed to Azure app service

    enter image description here

    Configure the below startup command in your Azure app service

    gunicorn --worker-class uvicorn.workers.UvicornWorker --timeout 600 --access-logfile '-' --error-logfile '-' app:app
    

    enter image description here

    Here's the output after deployment:

    enter image description here