---
django: 6.1.3 (f16e25dcb9fe55734ba4ea2b00ac34405b7df74a)
---

# manage.py, discovery, and BaseCommand

What runs between typing `python manage.py ...` and entering `BaseCommand.handle`, and why do some commands skip `django.setup` or system checks?

Every `"manage.py"` in every Django repository is practically identical: it sets `"DJANGO_SETTINGS_MODULE"` via `"os.environ.setdefault"` and hands `"sys.argv"` straight to **`execute_from_command_line`** (`django/core/management/__init__.py:execute_from_command_line`), which constructs **`ManagementUtility`** (`django/core/management/__init__.py:ManagementUtility`) and calls `ManagementUtility.execute` (`django/core/management/__init__.py:ManagementUtility.execute`).

## Bootstrapping settings, `get_commands`, and app override precedence

Trace the exact order inside `ManagementUtility.execute`:

1. **Early option pre-parsing:** Before importing settings or booting apps, Django creates a minimal `CommandParser` to extract `"--settings"` and `"--pythonpath"` so `"handle_default_options"` can mutate `"os.environ"` and `"sys.path"` early enough to affect which apps and commands exist.
2. **Conditional `django.setup()`:** If `"settings.configured"` is `True` (having just loaded `"INSTALLED_APPS"`), Django calls `django.setup()` — with one deliberate exception on lines 393–414: when running `"runserver"` without `"--noreload"`, `ManagementUtility.execute` wraps `django.setup` in `django.utils.autoreload.check_errors` and swallows startup exceptions by populating empty `"apps.app_configs"` so the parent autoreloader can still start, watch your broken file, and restart cleanly once you save the fix! Commands run without valid settings (like `"django-admin startproject"` or `"--version"`) skip `django.setup()` completely.
3. **Command discovery (`get_commands`, `django/core/management/__init__.py:get_commands`):** Scans `"django/core/management/commands/"` first, then iterates **`reversed(apps.get_app_configs())`** scanning each app's `"management/commands/"` directory. Walking `"INSTALLED_APPS"` in reverse means the **earliest** app in `"INSTALLED_APPS"` wins any name collision — which is exactly why `"django.contrib.staticfiles"` overrides `"django.core"`'s built-in `"runserver"` command to serve static files automatically in development!
4. **Command loading (`ManagementUtility.fetch_command`, `django/core/management/__init__.py:ManagementUtility.fetch_command`):** Imports `"<app>.management.commands.<name>"` via `load_command_class` and instantiates its `"Command"` subclass of **`BaseCommand`** (`django/core/management/base.py:BaseCommand`).

## `BaseCommand.run_from_argv`, `execute`, and check gating

Next `ManagementUtility` calls `BaseCommand.run_from_argv` (`django/core/management/base.py:BaseCommand.run_from_argv`), which parses the full `argparse` CLI flags, calls `BaseCommand.execute` (`django/core/management/base.py:BaseCommand.execute`), intercepts `CommandError` (`django/core/management/base.py:CommandError`) to print a clean error message and `sys.exit(e.returncode)` (unless `"--traceback"` was passed), and **always** executes `"connections.close_all()"` inside its `"finally:"` block so short-lived CLI scripts never leak open database sockets!

Inside `BaseCommand.execute`, two class attributes decide what guards run *before* your `BaseCommand.handle`:

- **`BaseCommand.requires_system_checks`:** Defaults to `ALL_CHECKS` (`"__all__"`), causing `BaseCommand.execute` to run `BaseCommand.check` before `handle()` unless the user passed `"--skip-checks"`. Commands that either *are* the checker (`"check"`), inspect raw settings (`"diffsettings"`), or scaffold new projects (`"startapp"`) set `requires_system_checks = []` so they do not fail or double-check before running. (Notice `"runserver"` keeps `"__all__"` on the class but overrides `get_check_kwargs` to defer checks into the autoreloaded child thread!)
- **`BaseCommand.requires_migrations_checks`:** Defaults to `False`; commands that warn about unapplied migrations before touching data can flip it to `True` to invoke `BaseCommand.check_migrations` automatically.

Watch `get_commands` and `ManagementUtility.fetch_command` inspect `"desk/"`'s `31` discovered commands and their exact check flags live:

```text recording=ch13-tooling
1. ManagementUtility command discovery and BaseCommand flags
  discovered_commands=31 runserver_app='django.contrib.staticfiles' migrate_app='django.core'
  requires_system_checks: check=[] migrate='__all__' runserver='__all__' diffsettings=[]
  requires_migrations_checks: migrate=False runserver=False check=False
```

Notice `"runserver_app='django.contrib.staticfiles'"` confirming reverse-`"INSTALLED_APPS"` command precedence, and `"check=[]"` avoiding recursive double-checking inside `"manage.py check"`.

Next we open that exact check engine — seeing how `CheckRegistry` separates everyday static checks from `"--deploy"` security guards and `"database"` checks — in [The system check framework and --deploy guards](checks.md).
