[Django]-How can I tell whether my Django application is running on development server or not?

22đź‘Ť

âś…

server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
    print('inside dev')

Of course, wsgi.file_wrapper might be set on META, and have a class from a module named django.core.servers.basehttp by extreme coincidence on another server environment, but I hope this will have you covered.

By the way, I discovered this by making a syntatically invalid template while running on the development server, and searched for interesting stuff on the Traceback and the Request information sections, so I’m just editing my answer to corroborate with Nate’s ideas.

👤inerte

48đź‘Ť

I put the following in my settings.py to distinguish between the standard dev server and production:

import sys
RUNNING_DEVSERVER = (len(sys.argv) > 1 and sys.argv[1] == 'runserver')

This also relies on convention, however.

(Amended per Daniel Magnusson’s comment)

19đź‘Ť

Usually this works:

import sys

if 'runserver' in sys.argv:
    # you use runserver
👤Sven R. Kunze

17đź‘Ť

Typically I set a variable called environment and set it to “DEVELOPMENT”, “STAGING” or “PRODUCTION”. Within the settings file I can then add basic logic to change which settings are being used, based on environment.

EDIT: Additionally, you can simply use this logic to include different settings.py files that override the base settings. For example:

if environment == "DEBUG":
    from debugsettings import *
👤Soviut

7đź‘Ť

Relying on settings.DEBUG is the most elegant way AFAICS as it is also used in Django code base on occasion.

I suppose what you really want is a way to set that flag automatically without needing you update it manually everytime you upload the project to production servers.

For that I check the path of settings.py (in settings.py) to determine what server the project is running on:

if __file__ == "path to settings.py in my development machine":
    DEBUG = True
elif __file__ in [paths of production servers]:
    DEBUG = False
else:
    raise WhereTheHellIsThisServedException()

Mind you, you might also prefer doing this check with environment variables as @Soviut suggests. But as someone developing on Windows and serving on Linux checking the file paths was plain easier than going with environment variables.

👤utku_karatas

4đź‘Ť

I came across this problem just now, and ended up writing a solution similar to Aryeh Leib Taurog’s. My main difference is that I want to differentiate between a production and dev environments when running the server, but also when running some one-off scripts for my app (which I run like DJANGO_SETTINGS_MODULE=settings python [the script] ). In this case, simply looking at whether argv[1] == runserver isn’t enough. So what I came up with is to pass an extra command-line argument when I run the devserver, and also when I run my scripts, and just look for that argument in settings.py. So the code looks like this:

if '--in-development' in sys.argv:
    ## YES! we're in dev
    pass
else:
    ## Nope, this is prod
    pass

then, running the django server becomes

python manage.py runserver [whatever options you want] –in-development

and running my scripts is as easy as

DJANGO_SETTINGS_MODULE=settings python [myscript] –in-development

Just make sure the extra argument you pass along doens’t conflict with anything django (in reality I use my app’s name as part of the argument).
I think this is pretty decent, as it lets me control exactly when my server and scripts will behave as prod or dev, and I’m not relying on anyone else’s conventions, other than my own.

EDIT: manage.py complains if you pass unrecognized options, so you need to change the code in settings.py to be something like

if sys.argv[0] == 'manage.py' or '--in-development' in sys.argv:
    # ...
    pass

Although this works, I recognize it’s not the most elegant of solutions…

4đź‘Ť

If you want to switch your settings files automatically dependent on the runtime environment
you could just use something that differs in environ, e.g.

from os import environ
if environ.get('_', ''): 
    print "This is dev - not Apache mod_wsgi"         
👤Ed C

4đź‘Ť

You can determine whether you’re running under WSGI (mod_wsgi, gunicorn, waitress, etc.) vs. manage.py (runserver, test, migrate, etc.) or anything else:

import sys
WSGI = 'django.core.wsgi' in sys.modules
👤OrangeDog

2đź‘Ť

settings.DEBUG could be True and running under Apache or some other non-development server. It will still run. As far as I can tell, there is nothing in the run-time environment short of examining the pid and comparing to pids in the OS that will give you this information.

👤hughdbrown

2đź‘Ť

I use:

DEV_SERVERS = [
    'mymachine.local',
]

DEVELOPMENT = platform.node() in DEV_SERVERS

which requires paying attention to what is returned by .node() on your machines. It’s important that the default be non-development so that you don’t accidentally expose sensitive development information.

You could also look into more complicated ways of uniquely identifying computers.

👤Carl G

1đź‘Ť

One difference between the development and deployment environment is going to be the server that it’s running on. What exactly is different will depend on your dev and deployment environments.

Knowing your own dev and deploy environments, the HTTP request variables could be used to distinguish between the two. Look at request variables like request.META.HTTP_HOST, request.META.SERVER_NAME and request.META.SERVER_PORT and compare them in the two environments.

I bet you’ll find something quite obvious that’s different and can be used to detect your development environment. Do the test in settings.py and set a variable that you can use elsewhere.

👤Nate

1đź‘Ť

Inspired by Aryeh’s answer, the trick I devised for my own use is to just look for the name of my management script in sys.argv[0]:

USING_DEV_SERVER = "pulpdist/manage_site.py" in sys.argv[0]

(My use case is to automatically enable Django native authentication when running the test server – when running under Apache, even on development servers, all authentication for my current project is handled via Kerberos)

👤ncoghlan

1đź‘Ť

You could check request.META["SERVER_SOFTWARE"] value:

dev_servers = ["WSGIServer", "Werkzeug"]
if any(server in request.META["SERVER_SOFTWARE"] for server in dev_servers):
    print("is local")

1đź‘Ť

Simple you may check the path you work on server. Something like:

import os
SERVER = True if os.path.exists('/var/www/your_project') else False
👤Cappittall

0đź‘Ť

One approach is to create an environment variable on your dev machine such as RUNNING_IN_DEVELOPMENT = 1. JetBrains, for example, lets you do this in the Configuration Dialog.

Then in settings.py you can do:

try:
    running_in_development = os.getenv('RUNNING_IN_DEVELOPMENT')
    DEBUG = True if running_in_development else False
except Exception as e:
    DEBUG = False
👤VikR

0đź‘Ť

I use the following:

MANAGING = (
    len(sys.argv) >= 2 and "manage.py" in sys.argv[0] and sys.argv[1] != "runserver"
)
👤wieczorek1990

Leave a comment