[IPython-user] __main__ vs __main__

Dave Grote dpgrote@lbl....
Thu Jan 3 16:11:33 CST 2008


Hello All,
  Is there a nice way of getting ipython to use the original __main__ 
rather than creating its own? In my code, I have python calling C code, 
which then calls python code, using PyRun_SimpleString. The 
PyRun_SimpleString will call, for example, a function defined in python 
before the C code is called. This works fine in plain python. But not in 
ipython - it by default creates its own name space which 
PyRun_SimpleString can't directly get at. I came up with the following, 
which seems to work ok,

import __main__
__main__.__name__ = "__mynamespace__"
import IPython
IPython.Shell.start(user_ns=__main__.__dict__).mainloop()

Setting __name__ is needed to prevent the ipython __main__ from being 
replaced by a FakeModule, which doesn't seem to interact with 
PyRun_SimpleString. Is this a sensible solution? Has anyone else done 
this kind of thing?
  Any and all comments welcome! Thanks!
      Dave


More information about the IPython-user mailing list