Skip to content

Instantly share code, notes, and snippets.

@Quotation
Forked from defrex/shell.py
Last active August 19, 2023 14:23
Show Gist options
  • Save Quotation/909f87320b03b028d23c to your computer and use it in GitHub Desktop.
Save Quotation/909f87320b03b028d23c to your computer and use it in GitHub Desktop.
Use ptpython for Django shell
from django.core.management.commands.shell import Command as ShellCommand
import os
class Command(ShellCommand):
shells = ShellCommand.shells.append('ptpython')
def ptpython(self):
try:
# old ptpython
from prompt_toolkit.contrib.repl import embed
except ImportError:
# new ptpython
from ptpython.repl import embed
history_filename = os.path.expanduser('~/.ptpython_history')
embed(globals(), locals(), vi_mode=False, history_filename=history_filename)
@namper
Copy link

namper commented Sep 10, 2022

Append is incorrect should be ShellCommand.shells + ['ptpython']

Full version:

from django.core.management.commands.shell import Command as ShellCommand
import os


class Command(ShellCommand):
    shells = ShellCommand.shells + ['ptpython']

    def ptpython(self, options):
        from ptpython.repl import embed

        history_filename = os.path.expanduser('~/.ptpython_history')
        embed(globals(), locals(), vi_mode=False, history_filename=history_filename)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment