providing a python shell from an app, with app defined objects available.

Bruce Edge bedge at
Thu Mar 22 20:42:45 CET 2001

I apologize for the length, but here's the simples example I can make:

file contains:
def run_python():
	banner = "Use Ctrl-D to exit subshell"	
	import code
	import __main__
	locals = __main__.__dict__
	x = code.InteractiveConsole(locals)
	x.interact( banner )


>>> from xx import *
Use Ctrl-D to exit subshell
>>> dir()
['__builtins__', '__doc__', '__name__', 'atexit', 'histfile', 
'os', 'readline', 'rlcompleter', 'savehist', 'sys']

Note: no run_python defined

Ctrl-D hit here, back to main shell.

>>> dir()
['__builtins__', '__doc__', '__name__', 'atexit', 'histfile', 
'os', 'readline', 'rlcompleter', -> 'run_python' <-, 'savehist', 'sys']

Now run_python is defined.

How can I get that function into the InteractiveConsole name space?

If I re-run run_python now:

>>> run_python()
Use Ctrl-D to exit python and return to CLI
>>> dir()
['__builtins__', '__doc__', '__name__', 'atexit', 'histfile', 
'os', 'readline', 'rlcompleter', 'run_python', 'savehist', 'sys']

It _is_ defined. But, I need it to work without having to drop back to
the main interpreter shell first.

This is needed to allow a user to get access to all the objects the app defines,
but never give them direct access to the main shell.

If this is a case of RTFM I would greatly appreciate a pointer to the
section of the FM.

Thanks, Bruce.

More information about the Python-list mailing list