13.1 Commands, checks, and testing
manage.py, discovery, and BaseCommand
What runs between typing python manage and entering BaseCommand, 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 and hands "sys.argv" straight to execute_from_command_line, which constructs ManagementUtility and calls ManagementUtility.
Bootstrapping settings, get_commands, and app override precedence
Trace the exact order inside ManagementUtility:
- Early option pre-parsing: Before importing settings or booting apps, Django creates a minimal
CommandParserto extract"--settings"and"--pythonpath"so"handle_default_options"can mutate"os.environ"and"sys.path"early enough to affect which apps and commands exist. - Conditional
django.setup(): If"settingsis.configured" True(having just loaded"INSTALLED_APPS"), Django callsdjango.setup()— with one deliberate exception on lines 393–414: when running"runserver"without"--noreload",ManagementUtilitywraps.execute django.setupindjangoand swallows startup exceptions by populating empty.utils .autoreload .check_errors "appsso the parent autoreloader can still start, watch your broken file, and restart cleanly once you save the fix! Commands run without valid settings (like.app_configs" "django-admin startproject"or"--version") skipdjango.setup()completely. - Command discovery (
get_commands,get_commands): Scans"django/first, then iteratescore/ management/ commands/ " reversed(appsscanning each app’s.get_app_configs()) "management/directory. Walkingcommands/ " "INSTALLED_APPS"in reverse means the earliest app in"INSTALLED_APPS"wins any name collision — which is exactly why"djangooverrides.contrib .staticfiles" "django.core"'s built-in"runserver"command to serve static files automatically in development! - Command loading (
ManagementUtility,.fetch_command ManagementUtility): Imports.fetch_command "<app>.managementvia.commands.<name>" load_command_classand instantiates its"Command"subclass ofBaseCommand.
BaseCommand.run_from_argv , execute, and check gating
Next ManagementUtility calls BaseCommand, which parses the full argparse CLI flags, calls BaseCommand, intercepts CommandError to print a clean error message and sys (unless "--traceback" was passed), and always executes "connections inside its "finally:" block so short-lived CLI scripts never leak open database sockets!
Inside BaseCommand, two class attributes decide what guards run before your BaseCommand:
BaseCommand: Defaults to.requires_system_checks ALL_CHECKS("__all__"), causingBaseCommandto run.execute BaseCommandbefore.check handle()unless the user passed"--skip-checks". Commands that either are the checker ("check"), inspect raw settings ("diffsettings"), or scaffold new projects ("startapp") setrequires_system_checks = []so they do not fail or double-check before running. (Notice"runserver"keeps"__all__"on the class but overridesget_check_kwargsto defer checks into the autoreloaded child thread!)BaseCommand: Defaults to.requires_migrations_checks False; commands that warn about unapplied migrations before touching data can flip it toTrueto invokeBaseCommandautomatically..check_migrations
Watch get_commands and ManagementUtility inspect "desk/"'s 31 discovered commands and their exact check flags live:
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 confirming reverse-"INSTALLED_APPS" command precedence, and "check=[]" avoiding recursive double-checking inside "manage.
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.