[IPython-user] Embedding IPython with py2exe

Fernando Perez Fernando.Perez at colorado.edu
Fri Dec 31 13:23:20 CST 2004


Michael Foord wrote:

> I've added an interactive interpreter to it (effectively replicating the
> '-' and '-i' command line options of python) and have provided the
> option of using IPython as the console if the distribution was built
> with IPython available.
> 
> I cam across the following issue when embedding IPython in a py2exe
> program. I thought it might be useful to share the information here in
> case any other users attempted to do a similar thing.
> 
> The function that calls interactive mode is :
> 
> 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()
>     sys.exit()
> 
> which is called with :
> interactive(locals())

Just as a minor side note, you could set localvars=None by default, and fetch 
them for the user using sys._getframe.  See how the IPShellEmbed's __call__ 
method works for details.  It's just a small, nice convenience for your users, 
so they can just say:

interactive()

and be done with it.  If the computer can figure out a sensible default, by 
all means let it do it :)

> Nice and easy !! Except when called from a py2exe program, site.py isn't
> imported. This means that 'copyright' and 'credits' aren't available as
> builtins. This causes part of IPython to fail. To rectify this I used
> the following code straight from site.py :

[...]

> This solves the problem neatly. I've also posted this message to the
> py2exe mailing list as well. It could also be fixed by testing for
> NameError when they are used in IPython... but that may not be a priority.

Well, I'm not exactly sure what your question is here.  Would you prefer 
ipython to perform this kind of check, or are you happy with your current 
solution?  I don't have a problem with adding such a check, esp. because other 
embedded users may run into the exact same problem down the road.  I tend to 
avoid too many special-purpose checks, but this one seems to provide wide 
benefit, and as I'm starting to work on the internal cleanup for GUI 
embedding, this may well become necessary.


Cheers,

f

ps. Good luck with your project, and thanks for including ipython!




More information about the IPython-user mailing list