Skip to main content

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

Hippocratic License 3.0

Metadata

Release files for django-run-context 1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-run-context 1.1
File Size Uploaded Provenance Actions
django_run_context-1.1.tar.gz 9.2 kB Without provenance Details for django_run_context-1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-run-context 1.1
File Interpreter ABI Platform Actions
django_run_context-1.1-py3-none-any.whl Python 3 none any Details for django_run_context-1.1-py3-none-any.whl

Total release size: 18.7 kB

Release files / django_run_context-1.1.tar.gz

Download URL django_run_context-1.1.tar.gz
Size 9.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e830c121409d58a51dbff9315ee233fa4bb7590f2b22dd1c0f49a299288f4613
BLAKE2b-256 checksum
How to use checksums
2581eb03c454b29b25e6e390c20b479cb157c2207a4802ba6dec4976b7fbcd31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / django_run_context-1.1-py3-none-any.whl

Download URL django_run_context-1.1-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e91572612e63f75b8821e81281dd5e3427551bc8bb1166fb2adb12a8a5d49eb
BLAKE2b-256 checksum
How to use checksums
baea72e680fff7a635097c317f937973a1e7242e3bd8635f3eb3c3b16120dca5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for django-run-context 1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page