django-run-context
Determine the context under which a Django project is being run.
Installation
pip install django-run-context
Supported contexts
get_run_context() returns one of:
| Context | Meaning |
|---|---|
"runserver_reloader" |
The runserver supervising process; watches files, serves no requests |
"runserver_reloaded" |
The webserver process spawned by the runserver reloader |
"runserver_noreloader" |
runserver started with --noreload |
"not runserver" |
Any other context, e.g. gunicorn, uwsgi, daphne, celery, ... |
Usage
Add it to your settings.py:
from django_run_context import get_run_context
RUN_CONTEXT = get_run_context()
and access it anywhere else in the project:
from django.conf import settings
print(settings.RUN_CONTEXT)
Why you might need this
By default manage.py runserver loads settings.py twice: once in the
supervising process (which only watches the filesystem for changes) and again
in the reloader-spawned process that actually serves requests. Anything printed
from settings.py — for example diagnostics in DEBUG mode — therefore
appears twice.
RUN_CONTEXT lets you emit such output exactly once, for example:
if DEBUG and RUN_CONTEXT != "runserver_reloader":
import django
import sys
log.debug(f"Django version: {django.__version__}")
log.debug(f"Python version: {sys.version}")
log.debug(f"Django loaded from: {django.__file__}")
log.debug(f"Using Path: {sys.path}")
log.debug(f"Static root: {STATIC_ROOT}")
log.debug(f"Static file dirs: {STATICFILES_DIRS}")
log.debug(f"Installed apps: {INSTALLED_APPS}")
log.debug(f"Database: {DATABASES['default']}")
log.debug(f"Command line: {sys.argv}")
The supervising (reloader) process suppresses the output, and the real server
prints it once — whether started with the reloader (the default) or with
--noreload.
Start management commands clean
Management commands (migrate, shell, custom commands, ...) all run under
"not runserver". A common companion use case: dump the full diagnostics only
when a server starts up, while keeping management command startups quiet.
A typical pattern in settings.py:
show_settings = "show_settings" in sys.argv
if DEBUG and RUN_CONTEXT not in ("runserver_reloader", "not runserver") or show_settings:
import django
import sys
log.debug("SETTINGS LOADED (in context '%s'):", RUN_CONTEXT)
log.debug("Python version: %s", sys.version)
log.debug("Python executable: %s", sys.executable)
log.debug("Using path: %s", sys.path)
log.debug("Django version: %s", django.__version__)
log.debug("Django loaded from: %s", django.__file__)
log.debug("Installed apps: %s", INSTALLED_APPS)
log.debug("Database: %s", DATABASES["default"])
log.debug("Command line: %s", sys.argv)
Here RUN_CONTEXT not in ("runserver_reloader", "not runserver") means the
dump is emitted:
- in the actual runserver webserver process (
"runserver_reloaded"/"runserver_noreloader"), not in the supervising reloader, so it appears only once; and - not in management commands, so they start clean.
Passing show_settings as an argument to any management command forces the
dump regardless of context:
python manage.py shell show_settings
python manage.py some_command show_settings
How it works
Django's autoreloader marks the spawned server process with the environment
variable RUN_MAIN (exposed as the private constant
django.utils.autoreload.DJANGO_AUTORELOAD_ENV) set to "true". This module
combines that flag with the presence of runserver and --noreload in
sys.argv to classify the current process.
Compatibility
- Python 3.7+
- Django 2.2+
License
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_run_context-1.1.tar.gz.
File metadata
- Download URL: django_run_context-1.1.tar.gz
- Upload date:
- Size: 9.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e830c121409d58a51dbff9315ee233fa4bb7590f2b22dd1c0f49a299288f4613
|
|
| MD5 |
fbe96ae8c09ccc84535603dffd6e10ce
|
|
| BLAKE2b-256 |
2581eb03c454b29b25e6e390c20b479cb157c2207a4802ba6dec4976b7fbcd31
|
File details
Details for the file django_run_context-1.1-py3-none-any.whl.
File metadata
- Download URL: django_run_context-1.1-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e91572612e63f75b8821e81281dd5e3427551bc8bb1166fb2adb12a8a5d49eb
|
|
| MD5 |
c5549a4df871d9e30e1365382f181643
|
|
| BLAKE2b-256 |
baea72e680fff7a635097c317f937973a1e7242e3bd8635f3eb3c3b16120dca5
|