This is handled by django.core.management.find_commands :
def find_commands(management_dir):
    """
    Given a path to a management directory, return a list of all the command
    names that are available.
    """
    command_dir = os.path.join(management_dir, "commands")
    return [
        name
        for _, name, is_pkg in pkgutil.iter_modules([command_dir])
        if not is_pkg and not name.startswith("_")
    ]
Originally implemented in https://github.com/django/django/commit/302eeaf1904a2f3852b223a563617d4999b4e9ae