Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

429
Views
Google Cloud Django if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'): not working

I am working on a Google Cloud Django Project.

I am trying to check in settings.py if I am running in Development or Production mode.

I added the following block of code to test if the SOFTWARE where the program is running either is on my machine or on Google Cloud Servers.

# | settings.py |
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    #code to execute in production
else:
    #code to execute in development

I noticed that the if statment is always false, so I decited to debug the os.environ dict. The result of the debug is that the value of the key SERVER_SOFTWARE of the enviroment is equal to gunicorn/20.1.0.

As written in the correct answer of this stackoverflow question, when running on production (so on Google Cloud Severs App Engine), the value of SERVER_SOFTWARE should be Google App Engine/X.Y.Z, where X, Y and Z represent the version of Google Cloud. But, as I said, my value, when running on App Engine, is not like that, it is gunicorn/20.1.0.

So, how do I make the program know if I am running either in development or production?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Unfortunately, the Stack Overflow (SO) answer you linked to is almost twelve years old and refers to Python 2 (deprecated). I was unable to find the SYSTEM_SOFTWARE default env. var. for both the Google Cloud Platform (GCP) App Engine Standard (here) and Flexible environment (here).

It's safe to say that the env. var. that App Engine injects into every Standard instance by default can change. Instead, why don't you supply an env. var. in your App Engine Standard env. configuration file (app.yaml) to make sure that your Django application is running in "production" only when you want it to:

  1. Add the env_variables section to app.yaml, such as:

    . . .
    env_variables:
      DJANGO_PRODUCTION_ENVIRONMENT: true
    . . .
    

    or:

    . . .
    env_variables:
      DJANGO_SYSTEM_SOFTWARE: "production"
    . . .
    

    You can look into more App Engine Standard env. config. settings here.

  2. Deploy your Django application to App Engine Standard env. with the GCP Cloud SDK (gcloud):

    gcloud app deploy --appyaml=./app.yaml
    

Confirm that older versions (e.g., $AES_OLD_VERSION) have stopped with:

gcloud app versions list \
--format="table[box=true](version.id:label=ID, version.servingStatus:label=STATUS)" \
--service=$AES_SERVICE_ID
┌──────────────────┬─────────┐
│        ID        │  STATUS │
├──────────────────┼─────────┤
│      . . .       │  . . .  │
│ $AES_OLD_VERSION │ STOPPED │
│ $AES_VERSION_ID  │ SERVING │
└──────────────────┴─────────┘

Confirm that your env. var. have made it into $AES_VERSION_ID with:

gcloud app versions describe $AES_VERSION_ID \
--format="yaml(envVariables)" \
--service=$AES_SERVICE_ID

#=>

envVariables:
  DJANGO_PRODUCTION_ENVIRONMENT: 'True'
  DJANGO_SYSTEM_SOFTWARE: production
over 4 years ago · Santiago Trujillo Report

0

I'll present a couple other solutions.

(1) GAE provided env vars

GAE automatically sets environment variables that you probably don't set when running locally. I use this:

version = os.environ.get('GAE_VERSION', 'local')

You'll get the actual version in production and 'local' on your machine.

(2) Check the request URL

This is Flask but there must be something similar in Django:

request.url_root == 'http://localhost:8080/'
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!