def python(self, options): import code # Set up a dictionary to serve as the environment for the shell. imported_objects = {} # We want to honor both $PYTHONSTARTUP and .pythonrc.py, so follow system # conventions and get $PYTHONSTARTUP first then .pythonrc.py. if not options['no_startup']: for pythonrc in OrderedSet([ os.environ.get("PYTHONSTARTUP"), os.path.expanduser('~/.pythonrc.py') ]): if not pythonrc: continue if not os.path.isfile(pythonrc): continue with open(pythonrc) as handle: pythonrc_code = handle.read() # Match the behavior of the cpython shell where an error in # PYTHONSTARTUP prints an exception and continues. try: exec(compile(pythonrc_code, pythonrc, 'exec'), imported_objects) except Exception: traceback.print_exc() # By default, this will set up readline to do tab completion and to read and # write history to the .python_history file, but this can be overridden by # $PYTHONSTARTUP or ~/.pythonrc.py. try: sys.__interactivehook__() except Exception: # Match the behavior of the cpython shell where an error in # sys.__interactivehook__ prints a warning and the exception and continues. print("Failed calling sys.__interactivehook__") traceback.print_exc() # Set up tab completion for objects imported by $PYTHONSTARTUP or # ~/.pythonrc.py. try: import readline import rlcompleter readline.set_completer( rlcompleter.Completer(imported_objects).complete) except ImportError: pass # Start the interactive interpreter. code.interact(local=imported_objects)
def main(*args): """Script entry point.""" args = list(args) if '-noimport' in args: args.remove('-noimport') env = None warn_type = 'Ignoring' else: import pywikibot args = pywikibot.handle_args(args) env = {'pywikibot': pywikibot} warn_type = 'Unknown' if args: print('{} arguments: {}\n' # noqa: T001 .format(warn_type, ', '.join(args))) # Various stuffs in Python 3.4+, such as history file. # This is defined in the site module of the Python Standard Library, # and usually called by the built-in CPython interactive shell. if hasattr(sys, '__interactivehook__'): sys.__interactivehook__() code.interact("""Welcome to the Pywikibot interactive shell!""", local=env)
#!/usr/bin/env python import os import sys if __name__ == '__main__': from django.core.management import execute_from_command_line os.environ['DJANGO_SETTINGS_MODULE'] = 'ls.joyous.tests.settings' os.environ['PYTHONSTARTUP'] = __file__ argv = sys.argv[:1] + ['shell'] + sys.argv[1:] execute_from_command_line(argv) elif __name__ == "builtins": # useful once we are in the shell import datetime as dt from django.utils import timezone from django.conf import settings import pytz import pprint import ls.joyous from ls.joyous.models import * from ls.joyous.utils.recurrence import * from ls.joyous.utils import manythings, telltime from ls.joyous.formats import ical L = list sys.displayhook = pprint.pprint sys.__interactivehook__() timezone.activate("Pacific/Auckland")