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
| Attribute | Type | Description |
|---|
| help | string = "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_checks | list = [] | A list of system checks that must be performed before the command is executed, which is empty for this shell command. |
| shells | list = ["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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| options | dict | A 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
| Name | Type | Description |
|---|
| options | dict | A 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
| Name | Type | Description |
|---|
| options | dict | A 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
| Type | Description |
|---|
list | A 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
| Name | Type | Description |
|---|
| options | dict | Configuration options that determine whether to skip imports or adjust verbosity of the output. |
Returns
| Type | Description |
|---|
dict | A 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
| Name | Type | Description |
|---|
| options | dict | The parsed command-line arguments that control the execution flow and shell selection. |