Can't read variable environment in AppSerivce

Chau Nam Ky Nguyen 5 Reputation points
2023-11-25T11:23:51.08+00:00

I set the environment variables in the image. However, when I try to read the environment in my Django app, I receive a key error:


import os

from django.core.wsgi import get_wsgi_application

WORKING_ENV = os.environ['WORKING_ENV']
setting_module = 'api_learning.deployment' if WORKING_ENV == 'Deployment' else 'api_learning.settings'

os.environ.setdefault('DJANGO_SETTINGS_MODULE', setting_module)

application = get_wsgi_application()

My setting in App Service. Not change anything else. User's image

Error from git hub build:

User's image

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,187 questions
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 17,166 Reputation points Microsoft Employee
    2023-12-03T21:18:48.58+00:00

    @Chau Nam Ky Nguyen This could be because the environment variable is not set correctly or it is not available in the environment where your app is running.

    To troubleshoot this issue, you can try the following steps:

    1. Check if the environment variable is set correctly in the App Service. You can do this by going to the Configuration blade of your App Service and checking if the environment variable is listed there.
    2. If the environment variable is not set, you can set it in the Configuration blade of your App Service. Make sure to restart your App Service after setting the environment variable.
    3. If the environment variable is set correctly, you can try to print all the environment variables in your Django app to see if the environment variable is available. You can do this by adding the following code to your Django app:
    import os
    
    for key, value in os.environ.items():
        print(key, value)
    
    
    

    This will print all the environment variables available in your Django app. You can check if the environment variable you are trying to read is listed there.

    1. If the environment variable is not available, it could be because it is not set correctly in the environment where your app is running. You can try to set the environment variable in the environment where your app is running. For example, if you are running your app in a Docker container, you can set the environment variable in the Dockerfile or in the docker-compose.yml file.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.