3

I'm trying to deploy simple django web ap to Azure App Service using CI/CD pipeline (the most basic one that is offered by Microsoft for app deployment- no changes from me). However I'm getting the following error:

2021-03-08T16:55:51.172914117Z   File "", line 219, in _call_with_frames_removed
2021-03-08T16:55:51.172918317Z   File "/home/site/wwwroot/deytabank_auth/wsgi.py", line 13, in 
2021-03-08T16:55:51.172923117Z     from django.core.wsgi import get_wsgi_application
2021-03-08T16:55:51.172927017Z ModuleNotFoundError: No module named 'django'

I checked other threads and tried doing all the things mentioned but it did not help, or I am missing something:

In wsgi.py I added:

import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' )
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../licenses_api')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../deytabank_auth')

from django.core.wsgi import get_wsgi_application


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'deytabank_auth.settings')

application = get_wsgi_application()

But still getting the same error, where django is not recognized. I can see that reuqirements.txt is being installed successfully and it has all the neccessary libraries there (including Django)

My CI/CD yaml file looks like this:

# Python to Linux Web App on Azure
# Build your Python project and deploy it to Azure as a Linux Web App.
# Change python version to one thats appropriate for your application.
# https://learn.microsoft.com/azure/devops/pipelines/languages/python

trigger:
- develop

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureServiceConnectionId: '***'

  # Web app name
  webAppName: 'DeytabankAuth'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Environment name
  environmentName: 'DeytabankAuth'

  # Project root folder. Point to the folder containing manage.py file.
  projectRoot: $(System.DefaultWorkingDirectory)

  # Python version: 3.7
  pythonVersion: '3.7'

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 -r requirements.txt
      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 version'

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

Maybe I need to configure something in the Azure App Service? But i am not sure exactly what.

Alex T
  • 3,529
  • 12
  • 56
  • 105
  • 1
    Please hide your important information like `azureServiceConnectionId` for your information's safety. – Doris Lv Mar 09 '21 at 01:59

2 Answers2

2

I have met this issue before, and the problem might be your deployment method. Not sure which one you use, but the classic deployment center below is being deprecated, try use the new deployment center. enter image description here Checked your workflow with the one worked on my side, there is nothing different. So I will post the correct step worked on my side for you to refer.

  1. check your project locally to make sure it could run successfully.

  2. Create a new web app (this is to make sure no damage on your web app) and navigate to the Deployment center page. enter image description here

  3. Go to your GitHub and navigate to GitHub Action page to see the log. enter image description here

  4. Test your web app and check the file structure on KuDu site: https://{yourappname}.scm.azurewebsites.net/wwwroot/ enter image description here

You could test by click the browse button like what I did. enter image description here If you want to run command, go to this site: https://{yourappname}.scm.azurewebsites.net/DebugConsole enter image description here


By the way, I post this link if you need deploy using DevOps.

Doris Lv
  • 3,083
  • 1
  • 5
  • 14
  • Im using Azure Pipelines as my CI/CD service. I dont see anything I can setup in settings in the Azure App services, because it says its controlled by azure pipelines. There however it seems everything is set correctly. The wwwroot is essentially the same as on your screenshot. The only thing I cant see is the `python manage.py runserver` command anywhere. – Alex T Mar 09 '21 at 08:30
  • I have update my answer with the part you mentioned. Please have a look. @AlexT – Doris Lv Mar 09 '21 at 08:41
  • I'm thnking that after packing into artifact, when the app is deployed it does not run from within the `antenv` virtual environment, thats why it does not see django. Any way I can run the environment in the app service? – Alex T Mar 09 '21 at 08:50
  • If you mean run the `antenv`, the answer is yes. Pipeline already did all the things to run your Django app. – Doris Lv Mar 09 '21 at 09:00
0

The possible reason for this question is that you don't have Django installed.

In the Microsoft-hosted agent ubuntu-latest, Django is not pre-installed. That is, you need to install it manually.

pip install Django==3.1.7

Click this document for detailed information about downloading Django.

Jane Ma-MSFT
  • 4,461
  • 1
  • 6
  • 12
  • But I have Django in Requirements.txt I can see that is being downloaded and packed in the Artifact, before being deployed to the app service. – Alex T Mar 09 '21 at 07:30