如何使用工作生成器表达式嵌入IPython?

joe*_*ker 5 python embedding ipython

当我按照说明嵌入IPython 0.10时,某些列表推导无法正常工作.我的全局命名空间发生了什么?

$ python
>>> import IPython.Shell
>>> IPython.Shell.IPShellEmbed()()
In [1]: def bar(): pass
   ...: 
In [2]: list(bar() for i in range(10))
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)

/tmp/<ipython console> 

/tmp/<ipython console> in <generator expression>([outmost-iterable])

NameError: global name 'bar' is not defined
Run Code Online (Sandbox Code Playgroud)

joe*_*ker 0

似乎可以工作,但 IPython 认为它是主程序。因此,在实例化 IPShell 后,崩溃会显示“哎呀,IPython 崩溃了”。

import IPython.Shell
ipshell = IPython.Shell.IPShell(argv=[], user_ns={'root':root})
ipshell.mainloop()
Run Code Online (Sandbox Code Playgroud)