Skip to main content

Command

This class provides a command-line interface for running a Python interactive interpreter with automatic access to project models and utilities. It supports multiple shell interfaces including IPython, bpython, and the standard Python shell, with options to execute specific commands or scripts directly. The class manages environment setup, including handling startup scripts and configuring namespaces for seamless interaction with the application's data layer.

Attributes

AttributeTypeDescription
helpstring = "Runs a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available. Any standard input is executed as code."Runs a Python interactive interpreter. Tries to use IPython or bpython, if one of them is available. Any standard input is executed as code.
requires_system_checkslist = []A list of system checks that must be performed before the command is executed, which is empty for this shell command.
shellslist = ["ipython", "bpython", "python"]A list of supported interactive interpreter interfaces to attempt to load, ordered by preference.

Methods


add_arguments()

@classmethod
def add_arguments(
parser: [CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)
)

Configures the command-line argument parser with options for shell selection, startup script suppression, and automatic import disabling.

Parameters

NameTypeDescription
parser[CommandParser](../../base/commandparser.md?sid=django_core_management_base_commandparser)The argument parser instance used to define command-line flags and options.

ipython()

@classmethod
def ipython(
options: dict
)

Launches an interactive IPython shell session populated with the project's namespace.

Parameters

NameTypeDescription
optionsdictA dictionary of command-line options used to configure the shell environment.

bpython()

@classmethod
def bpython(
options: dict
)

Launches an interactive bpython shell session populated with the project's namespace.

Parameters

NameTypeDescription
optionsdictA dictionary of command-line options used to configure the shell environment.

python()

@classmethod
def python(
options: dict
)

Launches a standard Python interactive interpreter, optionally loading startup scripts and configuring tab completion.

Parameters

NameTypeDescription
optionsdictA dictionary of command-line options, including flags to skip startup scripts.

get_auto_imports()

@classmethod
def get_auto_imports() - > list

Return a sequence of import paths for objects to be auto-imported.

Returns

TypeDescription
listA list of dotted strings representing modules and models to be imported into the shell namespace.

get_namespace()

@classmethod
def get_namespace(
options: dict
) - > dict

Constructs a dictionary of objects to be made available in the shell, including models and common Django utilities.

Parameters

NameTypeDescription
optionsdictConfiguration options that determine whether to skip imports or adjust verbosity of the output.

Returns

TypeDescription
dictA mapping of variable names to their imported objects for use in the interactive session.

handle()

@classmethod
def handle(
options: dict
)

Executes the command logic by either running a specific string, processing standard input, or opening an interactive shell.

Parameters

NameTypeDescription
optionsdictThe parsed command-line arguments that control the execution flow and shell selection.