# IPython

**IPython**（交互式Python）是一个用于交互计算的命令外壳，最初为[Python](https://www.edgepedia.org/zh/python-programming-language)编程语言开发，提供对象自省、富媒体显示、外壳语法、制表符补全和持久命令历史。<sup>[1](https://github.com/ipython/ipython)</sup> 它由一个增强的终端外壳、为Jupyter笔记本及其他前端执行Python代码的内核，以及一个构建于traitlets库之上的配置与扩展系统组成。<sup>[2](https://ipython.readthedocs.io/en/stable/)</sup><sup> • </sup><sup>[3](https://ipython.org/about/)</sup> IPython全部以修订版BSD许可证开源。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 创建时间 | 2001年，由当时科罗拉多大学博尔德分校的研究生费尔南多·佩雷斯（Fernando Pérez）创建<sup>[5](https://ipython.readthedocs.io/en/5.x/about/history.html)</sup> |
| 目的 | 一个用于交互式和探索性计算的综合环境，佩雷斯称这一目标优于`PYTHONSTARTUP`环境变量<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> |
| 主要组件 | 增强的交互式外壳；解耦的双进程内核模型；交互式并行计算架构（现为独立的ipyparallel包）<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> |
| 许可证 | 修订版BSD许可证<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> |
| Python要求 | Python 3.11或更新版本；IPython 8.16之后最低版本遵循SPEC-0<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup><sup> • </sup><sup>[1](https://github.com/ipython/ipython)</sup> |
| 与Jupyter的关系 | 自IPython 4.0（2015年）起，笔记本格式和消息传递协议等语言无关的部分归属Jupyter项目；IPython仍然是Python内核和终端接口<sup>[3](https://ipython.org/about/)</sup> |
| 主要特性 | 使用`?`和`??`进行对象自省、制表符补全、以`%`为前缀的魔术命令、持久输入历史、使用`!`访问系统外壳<sup>[2](https://ipython.readthedocs.io/en/stable/)</sup> |

## 历史

IPython由费尔南多·佩雷斯于2001年创建，他当时是科罗拉多大学博尔德分校的物理学家和研究生，希望获得Mathematica风格的提示符、访问先前输出的能力，以及一个优于`PYTHONSTARTUP`环境变量的灵活配置系统。<sup>[5](https://ipython.readthedocs.io/en/5.x/about/history.html)</sup> 如今的项目源自佩雷斯合并的三个早期成果：他自己的ipython原型、扬科·豪泽（Janko Hauser）的IPP（提供了容器代码和帮助系统），以及内森·格雷（Nathan Gray）的LazyPython（自动括号和自动引号语法以及详细彩色回溯即取自该项目）。<sup>[5](https://ipython.readthedocs.io/en/5.x/about/history.html)</sup> 佩雷斯将他的目标描述为一个结合了类外壳特性、IDL和Matlab风格数值计算、Mathematica式提示历史以及强大对象自省的工作环境。<sup>[5](https://ipython.readthedocs.io/en/5.x/about/history.html)</sup>

2004年，当时在[圣克拉拉大学](https://www.edgepedia.org/zh/santa-clara-university)任教授的布莱恩·格兰杰（Brian Granger）加入项目并主导了其并行计算能力的开发。<sup>[6](https://ipython.org/_static/sloangrant/sloan-grant.pdf)</sup> 佩雷斯和格兰杰后来在2007年发表于《计算科学与工程》（*Computing in Science & Engineering*）的论文《IPython：交互式科学计算系统》中描述了该系统，将IPython定位为一个增强外壳、一套交互式并行与分布式计算工具，以及一个构建专用交互环境的工具包。<sup>[7](https://www.researchgate.net/publication/3422938_IPython_A_System_for_Interactive_Scientific_Computing)</sup>

**分裂为Jupyter。** 到IPython 3.0时，代码库包含了交互式外壳、REPL协议、笔记本文档格式和转换工具、基于网页的笔记本、小部件工具以及并行计算机制，其中大部分并非Python专属。<sup>[8](https://blog.jupyter.org/posts/2015/the-big-split/)</sup> 2014年在SciPy大会上宣布的Jupyter项目成为这些语言无关组件的新家。<sup>[8](https://blog.jupyter.org/posts/2015/the-big-split/)</sup> 分裂于2015年围绕IPython 4.0实施：`IPython.parallel`成为ipyparallel，`IPython.html`成为notebook，`IPython.nbconvert`和`IPython.nbformat`成为独立包，而`IPython.kernel`的内核端和客户端代码分别成为ipykernel和jupyter_client。<sup>[3](https://ipython.org/about/)</sup><sup> • </sup><sup>[8](https://blog.jupyter.org/posts/2015/the-big-split/)</sup> IPython现在专注于作为Jupyter内核和终端接口的交互式Python计算。<sup>[3](https://ipython.org/about/)</sup>

## 交互式外壳

通过`ipython`命令启动的终端外壳被设计为标准Python解释器的直接替代品：任何有效的Python代码都应能在其下正常执行，偏差被视为缺陷。<sup>[9](https://ipython.readthedocs.io/en/stable/interactive/reference.html)</sup> 其特性远超默认REPL：

- **对象自省。** 在对象后附加`?`可显示其文档字符串、函数签名、源文件及其他详细信息；`??`提供源代码等额外细节。通过`?`系统和`%psearch`命令可对命名空间进行通配符搜索。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup>
- **编号提示符和历史。** 输入和输出提示符均有编号（`In [n]` / `Out[n]`），所有输入均被缓存，历史在同一配置档案的多个会话间持久保存。先前的输入可作为变量使用：`_i`、`_ii`和`_iii`保存最近三次输入，列表`In`（亦作`_ih`）保存全部输入，每行还可通过`_i<n>`访问，因此第14号提示符的输入可通过`_i14`、`_ih[14]`或`In[14]`调回。<sup>[9](https://ipython.readthedocs.io/en/stable/interactive/reference.html)</sup>
- **制表符补全。** 按下TAB可补全关键字、模块、方法、变量和当前目录中的文件，由prompt_toolkit库实现；可添加自定义补全器。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> 终端接口还提供真正的多行编辑和输入时语法高亮。<sup>[2](https://ipython.readthedocs.io/en/stable/)</sup>
- **系统外壳访问。** 以`!`开头的行被传递给操作系统外壳，`!!`或`var = !cmd`将输出捕获到Python变量中，前缀为`$`的Python变量在外壳命令内部展开。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup>
- **调试器和性能分析器。** 集成了pdb调试器和Python性能分析器，`--pdb`选项开启异常的自动事后调试。<sup>[2](https://ipython.readthedocs.io/en/stable/)</sup><sup> • </sup><sup>[10](https://ipython.readthedocs.io/en/latest/config/intro.html)</sup>

**嵌入。** 在程序内调用`IPython.embed()`会在该处启动一个可访问当前命名空间的交互式外壳，这对于调试以及混合批处理与探索非常有用。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup><sup> • </sup><sup>[11](https://ipython.readthedocs.io/en/stable/api/generated/IPython.html)</sup> 在嵌入外壳中运行的代码仅当外壳位于全局命名空间时才改变周围变量；在函数内部，嵌入会话中的重新赋值不会传播回调用者的局部变量。<sup>[9](https://ipython.readthedocs.io/en/stable/interactive/reference.html)</sup>

## 魔术命令

首字符为`%`的行被视为对“魔术”函数的调用，这是一个用于控制IPython本身和执行系统类任务的命令系统。<sup>[12](https://ipython.readthedocs.io/en/latest/interactive/reference.html)</sup> 行魔术将行的其余部分作为参数；以`%%`为前缀的单元魔术还额外接收块中所有后续行作为单个字符串，该字符串甚至不必是有效的Python代码。<sup>[12](https://ipython.readthedocs.io/en/latest/interactive/reference.html)</sup>

启用“自动魔术”（此为默认设置）时，行魔术可省略`%`前缀调用，自动魔术系统在名称解析中优先级最低，因此名为`cd`的变量会遮蔽`%cd`魔术，直到该变量被删除或使用显式的`%cd`形式。<sup>[12](https://ipython.readthedocs.io/en/latest/interactive/reference.html)</sup> 示例包括：`%cd`用于目录导航，配有持久书签系统（`%bookmark`）；`%run`用于执行脚本并将其数据加载到交互命名空间；`%timeit`用于计时；`%alias`用于定义系统命令别名；`%history`及`%hist -g`用于搜索过去的输入。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup><sup> • </sup><sup>[9](https://ipython.readthedocs.io/en/stable/interactive/reference.html)</sup><sup> • </sup><sup>[13](https://ipython.readthedocs.io/en/3.x/interactive/tutorial.html)</sup> 魔术命令是提供它们的内核所特有的：IPython内核使用`%`是因为它在Python中不是有效的单目运算符，其他内核可能定义自己的约定。<sup>[14](https://ipython.readthedocs.io/en/5.x/interactive/magics.html)</sup>

## 双进程模型与内核

IPython通过将求值解耦到称为内核的独立进程来扩展传统的[读取-求值-打印循环](https://www.edgepedia.org/zh/read-eval-print-loop)，内核从客户端接收执行指令并向它们返回结果。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> 多个客户端可连接到同一内核，客户端和内核可运行在不同机器上。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> 只有传统的单进程终端外壳绕过此模型；其他一切，包括`jupyter console`、`jupyter qtconsole`和`jupyter notebook`，都使用它。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> 前端使用通过ZeroMQ套接字发送的JSON消息与内核通信，该协议记录在Jupyter消息传递规范中。<sup>[15](https://ipython.readthedocs.io/en/4.x/development/how_ipython_works.html)</sup> 客户端可通过`--existing`标志连接到已在运行的内核，`%connect_info`魔术会打印标识特定内核的连接文件。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup>

与兼容前端配合使用时，内核可显示HTML、图像、LaTeX、声音和视频等富输出，并通过ipywidgets包支持交互式小部件。<sup>[2](https://ipython.readthedocs.io/en/stable/)</sup> `IPython.display`模块为这些MIME类型定义了显示对象，包括`HTML`、`Image`、`SVG`、`JSON`、`Markdown`、`Latex`和`Video`，以及一个`display()`函数，将所有计算出的表示发送给前端，由前端选择渲染哪一种；在终端IPython中，结果回退为文本表示。<sup>[16](https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html?highlight=svg)</sup>

## 绘图与matplotlib集成

`%matplotlib`魔术为交互式使用设置matplotlib而不向命名空间导入任何内容；无参数调用时使用默认后端，也可请求特定后端，例如`%matplotlib qt`或`%matplotlib inline`。<sup>[17](https://ipython.readthedocs.io/en/latest/api/generated/IPython.core.magics.pylab.html)</sup><sup> • </sup><sup>[18](https://ipython.readthedocs.io/en/4.x/interactive/plotting.html)</sup> `inline`后端在[Jupyter Notebook](https://www.edgepedia.org/zh/jupyter-notebook)和Qt控制台中可用，将图表渲染为静态图形直接显示在生成它们的代码单元格下方，并存储在笔记本文档中。<sup>[18](https://ipython.readthedocs.io/en/4.x/interactive/plotting.html)</sup> 较旧的`%pylab`魔术更进一步，加载numpy和matplotlib并将其名称导入交互命名空间，`--no-import-all`限制星号导入。<sup>[17](https://ipython.readthedocs.io/en/latest/api/generated/IPython.core.magics.pylab.html)</sup>

## 配置与配置档案

IPython通过Python或JSON文件、命令行参数、`%config`魔术或以编程方式传递的traitlets `Config`对象进行配置。<sup>[10](https://ipython.readthedocs.io/en/latest/config/intro.html)</sup> 配置、命令历史和扩展位于IPython目录中，默认为`~/.ipython`，可通过`IPYTHONDIR`环境变量或`--ipython-dir`标志覆盖。<sup>[10](https://ipython.readthedocs.io/en/latest/config/intro.html)</sup> 配置档案为不同目的保存独立的配置和历史，例如一个配置档案用于NumPy和SciPy的数值计算，另一个用于SymPy的符号计算；`ipython profile create <name>`创建配置档案，`ipython --profile=<name>`使用它。<sup>[19](https://ipython.readthedocs.io/en/stable/development/config.html)</sup> 每个可配置值也可在命令行上设置为`ipython --ClassName.attribute=value`，常用选项有简短别名如`--matplotlib`和`--pdb`；命令行设置覆盖配置文件。<sup>[10](https://ipython.readthedocs.io/en/latest/config/intro.html)</sup> 系统级配置可部署在`/etc/ipython/`等位置以供多用户环境使用，较后的位置优先，以便用户覆盖系统默认值。<sup>[10](https://ipython.readthedocs.io/en/latest/config/intro.html)</sup>

## 安装与要求

IPython通过`pip install ipython`安装，通过安装ipykernel并运行`python -m ipykernel install`注册匹配的Jupyter内核。<sup>[20](https://ipython.readthedocs.io/en/stable/install/index.html)</sup> 当前版本要求Python 3.11或更新版本，可在Linux、macOS、Windows及大多数其他[类Unix](https://www.edgepedia.org/zh/unix-like)系统上运行。<sup>[4](https://ipython.readthedocs.io/en/stable/overview.html)</sup> IPython 8.16之后最低支持的Python版本遵循SPEC 0，即科学Python社区的版本支持政策，Python 3.11支持由德劭集团资助维持。<sup>[1](https://github.com/ipython/ipython)</sup> 撰写时的文档描述的是9.17.1版本。<sup>[2](https://ipython.readthedocs.io/en/stable/)</sup>

## References

1. [ipython/ipython repository README, GitHub](https://github.com/ipython/ipython)
2. [IPython Documentation, release 9.17.1](https://ipython.readthedocs.io/en/stable/)
3. [About IPython, ipython.org](https://ipython.org/about/)
4. [Overview, IPython 9.17.1 documentation](https://ipython.readthedocs.io/en/stable/overview.html)
5. [History, IPython 5.x documentation](https://ipython.readthedocs.io/en/5.x/about/history.html)
6. [Open Source Tools for Interactive, Collaborative and Reproducible Computing (Sloan grant report)](https://ipython.org/_static/sloangrant/sloan-grant.pdf)
7. [IPython: A System for Interactive Scientific Computing, Pérez & Granger, Computing in Science & Engineering (2007)](https://www.researchgate.net/publication/3422938_IPython_A_System_for_Interactive_Scientific_Computing)
8. [The Big Split™, Jupyter Blog (Min RK, 2015)](https://blog.jupyter.org/posts/2015/the-big-split/)
9. [IPython reference, IPython 9.17.1 documentation](https://ipython.readthedocs.io/en/stable/interactive/reference.html)
10. [Introduction to IPython configuration, IPython documentation](https://ipython.readthedocs.io/en/latest/config/intro.html)
11. [IPython module API, IPython 9.17.1 documentation](https://ipython.readthedocs.io/en/stable/api/generated/IPython.html)
12. [Magic command system, IPython reference](https://ipython.readthedocs.io/en/latest/interactive/reference.html)
13. [Introducing IPython, IPython 3.x documentation](https://ipython.readthedocs.io/en/3.x/interactive/tutorial.html)
14. [Built-in magic commands, IPython 5.x documentation](https://ipython.readthedocs.io/en/5.x/interactive/magics.html)
15. [How IPython works, IPython 4.x documentation](https://ipython.readthedocs.io/en/4.x/development/how_ipython_works.html)
16. [Module: display, IPython 9.17.1 documentation](https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html?highlight=svg)
17. [Module: core.magics.pylab, IPython documentation](https://ipython.readthedocs.io/en/latest/api/generated/IPython.core.magics.pylab.html)
18. [Plotting, IPython 4.x documentation](https://ipython.readthedocs.io/en/4.x/interactive/plotting.html)
19. [Overview of the IPython configuration system, IPython 9.17.1 documentation](https://ipython.readthedocs.io/en/stable/development/config.html)
20. [Installation, IPython 9.17.1 documentation](https://ipython.readthedocs.io/en/stable/install/index.html)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Development tools and collaboration infrastructure › Build, deployment, and development tooling

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/ipython

English original: https://www.edgepedia.org/ipython
