Jupyter 简明教程

Embedding IPython

IPython 模块的 embed() 函数使得在 Python 代码的名称空间中嵌入 IPython 成为可能。因此,你可以在默认的 Python 环境中利用 IPython 的特性,例如对象自省和 tab 补全。

The embed() function of IPython module makes it possible to embed IPython in your Python codes’ namespace. Thereby you can leverage IPython features like object introspection and tab completion, in default Python environment.

ipython module

在嵌入之前存在于全局名称空间的 Python 对象将可供 IPython 使用。

Python objects present in the global namespace before embedding, will be available to IPython.

python objects

如果在 IPython 中创建了新对象或修改了以前的对象,则在退出 IPython 后,它们将自动可供默认环境使用。嵌入的 IPython shell 不会改变先前代码或对象的状态。

If new objects are formed while in IPython or previous objects are modified, they will be automatically available to default environment after exiting IPython. Embedded IPython shell doesn’t change the state of earlier code or objects.

但是,如果 IPython 嵌入在函数内的局部名称空间中,则其中的对象在关闭后将不可用。在这里,我们定义了一个 add() 函数。在 add() 内部,我们调用 IPython 声明一个变量。如果我们尝试在 IPython 关闭后访问变量,将引发 NameError 异常。

However, if IPython is embedded in local namespace like inside a function, the objects inside it will not be available once it is closed. Here, we have defined a function add(). Inside add() we invoke IPython and declared a variable. If we try to access variable in IPython after it is closed, NameError exception will be raised.

ipython nameerror exception