IPython Pygments Lexers

raw JSON →
1.1.1 verified Tue May 12 auth: no python install: verified quickstart: verified

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.

pip install ipython-pygments-lexers
error WARNING: Pygments lexer name 'ipython3' is not known
cause 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.
fix
Ensure 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.
error ModuleNotFoundError: No module named 'ipython_pygments_lexers'
cause The `ipython-pygments-lexers` package has not been installed in the Python environment currently in use.
fix
Install the package using pip: pip install ipython-pygments-lexers
error ImportError: No module named pygments
cause 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.
fix
Install the Pygments library: pip install Pygments
breaking The Pygments lexers for IPython were extracted from the main `IPython` package (specifically `IPython.lib.lexers`) into this separate `ipython-pygments-lexers` package. Direct imports from `IPython.lib.lexers` will fail if IPython 9.0 or newer is used without this package being installed.
fix Install `ipython-pygments-lexers` and update import statements from `from IPython.lib.lexers import ...` to `from ipython_pygments_lexers import ...`.
gotcha When using Pygments' `get_lexer_by_name()` or similar lookup functions, the aliases 'ipython' and 'ipython3' may be confusing. Historically, these referred to the `IPyLexer` or `IPythonConsoleLexer`, which handle full console sessions. However, the alias 'ipython' can be misleading as an IPython console session (with prompts/outputs) is distinct from pure IPython code.
fix Prefer directly importing `IPyLexer` and instantiating it, or understand that the 'ipython' alias refers to the combined console lexer functionality rather than just pure code. For pure input code, `IPythonLexer` can be used.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.25s 26.3M
3.10 slim (glibc) - - 0.14s 27M
3.11 alpine (musl) - - 0.29s 29.0M
3.11 slim (glibc) - - 0.24s 30M
3.12 alpine (musl) - - 0.31s 20.8M
3.12 slim (glibc) - - 0.33s 21M
3.13 alpine (musl) - - 0.17s 20.5M
3.13 slim (glibc) - - 0.21s 21M
3.9 alpine (musl) - - 0.10s 25.8M
3.9 slim (glibc) - - 0.14s 26M

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)