From: Michael F. <mi...@pc...> - 2004-12-31 10:45:54
|
Python is way cool. Embedding an interactive interpreter into your programs is as easy as : def interactive(localvars): """A very simple function to embed an interactive interpreter into movpy.""" IPShellEmbed = None try: from IPython.Shell import IPShellEmbed except ImportError: pass if not IPShellEmbed or IPOFF: from code import InteractiveConsole con = InteractiveConsole(localvars) con.interact() else: ipshell = IPShellEmbed() ipshell() IPOFF = False # set to True to disable IPython use interactive(locals()) If you want to allow the use of IPython in a py2exe program you will also need to include something like the following code : ########################################### # Functions from site.py # Used by IPython # These add objects would normally be available as builtins in the python environment # but aren't in a normal py2exe program ! import __builtin__ def setquit(): """Define new built-ins 'quit' and 'exit'. These are simply strings that display a hint on how to exit. """ if os.sep == ':': exit = 'Use Cmd-Q to quit.' elif os.sep == '\\': exit = 'Use Ctrl-Z plus Return to exit.' else: exit = 'Use Ctrl-D (i.e. EOF) to exit.' __builtin__.quit = __builtin__.exit = exit class _Printer(object): """interactive prompt objects for printing the license text, a list of contributors and the copyright notice.""" MAXLINES = 23 def __init__(self, name, data, files=(), dirs=()): self.__name = name self.__data = data self.__files = files self.__dirs = dirs self.__lines = None def __setup(self): if self.__lines: return data = None for dir in self.__dirs: for filename in self.__files: filename = os.path.join(dir, filename) try: fp = file(filename, "rU") data = fp.read() fp.close() break except IOError: pass if data: break if not data: data = self.__data self.__lines = data.split('\n') self.__linecnt = len(self.__lines) def __repr__(self): self.__setup() if len(self.__lines) <= self.MAXLINES: return "\n".join(self.__lines) else: return "Type %s() to see the full %s text" % ((self.__name,)*2) def __call__(self): self.__setup() prompt = 'Hit Return for more, or q (and Return) to quit: ' lineno = 0 while 1: try: for i in range(lineno, lineno + self.MAXLINES): print self.__lines[i] except IndexError: break else: lineno += self.MAXLINES key = None while key is None: key = raw_input(prompt) if key not in ('', 'q'): key = None if key == 'q': break def setcopyright(): """Set 'copyright' and 'credits' in __builtin__""" __builtin__.copyright = _Printer("copyright", sys.copyright) __builtin__.credits = _Printer("credits", """\ Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands for supporting Python development. See www.python.org for more information.""") here = os.path.dirname(os.__file__) __builtin__.license = _Printer( "license", "See http://www.python.org/%.3s/license.html" % sys.version, ["LICENSE.txt", "LICENSE"], [os.path.join(here, os.pardir), here, os.curdir]) class _Helper(object): """Define the built-in 'help'. This is a wrapper around pydoc.help (with a twist). """ def __repr__(self): return "Type help() for interactive help, " \ "or help(object) for help about object." def __call__(self, *args, **kwds): import pydoc return pydoc.help(*args, **kwds) def sethelper(): __builtin__.help = _Helper() setquit() setcopyright() sethelper() ######################################### The *disadvantage* of all the code as above is that IPython does a conditional import of part of wx/wxpython. This means you could have some unnecessary wx/wxpython files in your library zipfile. (Not the whole wx distribution). Hope someone finds it interesting. Regards, Fuzzy http://www.voidspace.org.uk/python/index.shtml |