5

Possible Duplicate:
In Python, how can I test if I'm in Google App Engine SDK?

Is there an environment flag that will tell if my code is running in production or on the development server?

I'd like to be able to use an if statement to do something different depending on this flag.

(Specific case: newlines in a text file I want to read are treated differently in the two environments.)

Community
  • 1
  • 1
jonpalin
  • 93
  • 1
  • 5

2 Answers2

17
if os.environ.get('SERVER_SOFTWARE','').startswith('Development'):
    DEBUG = True
    logging.debug("[*] Debug info activated")
Birt
  • 437
  • 1
  • 6
  • 11
  • 2
    **thats a way to go** - too bad google does not provide a simple funncion like that one :( Just look after SERVER_SOFTWARE in google_appengine SDK and you will find ~8 calls doing that test – lukmdo Sep 29 '11 at 17:20
  • The `$SERVER_SOFTWARE` env. var. seems to be missing from App Engine Standard ([here](https://cloud.google.com/appengine/docs/standard/python3/runtime#environment_variables)) and Flexible ([here](https://cloud.google.com/appengine/docs/flexible/python/runtime#environment_variables)) default env. var. in 2021. Use `$GAE_ENV` if you **must** depend on a default env. var. I would **highly** recommend defining env. var. in your `app.yaml` file (further reading [here](https://cloud.google.com/appengine/docs/standard/python3/config/appref)). – Mike Nov 21 '21 at 23:27
1

For Google App Engine, since I've been told that you can't import socket you could probably use that fact to determine if you're running on GAE. This solution isn't 100% foolproof, but it should do what you want. Put this in settings.py.

try:
    import socket
except ImportError:
    DEBUG = TRUE

For anyone using Django but not GAE, you can use this instead. Change 'devserver' to whatever the hostname of your development server is.

import socket
if socket.gethostname() == 'devserver':
    DEBUG = TRUE

Then wherever you need to check the DEBUG variable in your code

from django.conf import settings

if settings.DEBUG:
    newline = '\n'
jonescb
  • 22,013
  • 7
  • 46
  • 42
  • I don't think app engine allows you to import socket. You may want to edit your post so the wrong information isn't spread. (I upvoted you because I don't think people should downvote and not give a reason.) – mcotton Jan 25 '11 at 02:03
  • Oh thanks. I don't use GAE, but this does work for plain Django. I didn't realize how limited GAE was. – jonescb Jan 25 '11 at 17:27