Registry /
serialization / ipython-pygments-lexers
The ipython-pygments-lexers library provides specialized Pygments lexers for syntax highlighting IPython code and interactive sessions. These lexers were originally part of the main IPython project but were later extracted into a dedicated package to reduce IPython's core dependencies. The package is actively maintained, with its current version being 1.1.1, and releases occur infrequently as needed for bug fixes or minor enhancements.
Install & Compatibility
Where this runs
tested against v1.1.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.925 runs
installs and imports cleanly · install 0.0s · import 0.194s · 26.3MB
glibcpy 3.10–3.925 runs
installs and imports cleanly · install 2.1s · import 0.176s · 27MB
25MB installed
● package 25MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
IPythonLexer
✓ from ipython_pygments_lexers import IPythonLexer
Lexer for pure IPython code (Python + magic/shell commands).
IPythonConsoleLexer
✓ from ipython_pygments_lexers import IPythonConsoleLexer
Lexer for full IPython console sessions, including prompts, output, and tracebacks.
IPyLexer
✓ from ipython_pygments_lexers import IPyLexer
✗ from IPython.lib.lexers import IPyLexer
This intelligent lexer (recommended for most general uses) determines whether to use IPythonLexer or IPythonConsoleLexer based on the input text. The old import path is from the monolithic IPython package before lexers were split out.
This quickstart demonstrates how to use the recommended `IPyLexer` from `ipython-pygments-lexers` with Pygments' `highlight` function and an `HtmlFormatter` to highlight a typical IPython console session. The `IPyLexer` intelligently adapts to the content, correctly rendering input prompts, outputs, and magic commands.
from pygments import highlight
from pygments.formatters import HtmlFormatter
from ipython_pygments_lexers import IPyLexer
code_to_highlight = (
"""In [1]: import numpy as np
...: a = np.array([1, 2, 3])
...: print(a * 2)
Out[1]: [2 4 6]
In [2]: %timeit [i**2 for i in range(1000)]
40.7 µs ± 1.15 µs per loop (mean ± Std. dev. of 7 runs, 10000 loops each)
"""
)
lexer = IPyLexer()
formatter = HtmlFormatter()
highlighted_code = highlight(code_to_highlight, lexer, formatter)
print(highlighted_code)
Errors
Common errors & fixes
WARNING: Pygments lexer name 'ipython3' is not known
This warning typically arises when the Pygments syntax highlighter cannot find the lexer named 'ipython3', often due to an outdated or incompatible version of IPython, or if the `ipython-pygments-lexers` package is not correctly registering its lexers with Pygments.
fixEnsure `ipython-pygments-lexers` is installed. If the issue persists, consider pinning IPython to a version known to be compatible (e.g., `ipython!=8.7.0`). If using Sphinx, ensure `ipython_pygments_lexers` (or `IPython.sphinxext.ipython_console_highlighting` for older setups) is added to the `extensions` list in your `conf.py` file.
ModuleNotFoundError: No module named 'ipython_pygments_lexers'
The `ipython-pygments-lexers` package has not been installed in the Python environment currently in use.
fixInstall the package using pip: `pip install ipython-pygments-lexers`
ImportError: No module named pygments
The core `Pygments` library, which `ipython-pygments-lexers` depends on for its functionality, is not installed or is not accessible in the active Python environment.
fixInstall the `Pygments` library: `pip install Pygments`
Audit
Dependencies
PygmentsrequiredProvides the core syntax highlighting framework that these lexers extend.
pythonrequiredRequires Python 3.8 or newer to run.