Question

In django framework you may run

./manage.py shell

and you'll be inside interactive shell with django settings. I can run a python interactive console using

#!/usr/bin/env python
import code

code.interact(local=locals())

I want to do the same as django does - execute python script to get inside the interactive shell (with my local thrift packages already imported - that's the clue).

Was it helpful?

Solution

I'm not really sure what you mean by "with my local thrift packages already imported", but if you want to start an interactive shell with a specific set of symbols already defined in the global namespace, I'd do it in a function, and import the symbols in there, to avoid polluting the sub-shell with any unwanted symbols from the rest of your code.

For example, if you wanted to start an interactive shell with sys and os already imported, then something like this ought to work...

>>> import code
>>> def start_shell():
...     import sys, os
...     code.interact(local=locals())
...
>>> start_shell()
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> dir()
['__builtins__', 'os', 'sys']

...noting that sys and os are defined, but not code because it was imported into the global namespace, not the local one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top