【问题标题】:Using IPython from the Python shell like `code.interact()`在 Python shell 中使用 IPython,例如 `code.interact()`
【发布时间】:2011-03-26 07:44:47
【问题描述】:

是否可以将现有 Python shell 中的 IPython shell 用作 shell-inside-a-shell,类似于内置的 code.interact()

【问题讨论】:

    标签: python shell ipython


    【解决方案1】:

    IPython 0.11 中对 API 进行了全面检查,并且 shell 更易于调用:

    import IPython
    
    IPython.embed()
    

    【讨论】:

      【解决方案2】:

      嵌入 IPython 的 recommended way 工作正常:

      ~ $ python
      Python 2.7 [...]
      >>> from IPython.Shell import IPShellEmbed
      >>> ipshell = IPShellEmbed()
      >>> ipshell()
      
      In [1]: 
      

      【讨论】:

        【解决方案3】:

        Django manage.py shell 尽可能调用 IPython shell,它的实现如下:

        import IPython
        
        shell = IPython.Shell.IPShell()
        shell.mainloop()
        

        【讨论】: