Registry / serialization / ipython-pygments-lexers

ipython-pygments-lexers

JSON →
library1.1.1pypypi✓ verified 52d ago

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.

serialization
pip install ipython-pygments-lexers
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
musl
py 3.103.925 runs
installs and imports cleanly · install 0.0s · import 0.194s · 26.3MB
glibc
py 3.103.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)
Debug
Known issues
breakingThe 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 ...`.
affects: IPython >= 9.0
gotchaWhen 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.
affects: All versions
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.
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.
ModuleNotFoundError: No module named 'ipython_pygments_lexers'
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`
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.
fix
Install the `Pygments` library: `pip install Pygments`
Upgrade
Version history
1.1.1latest on PyPI
Audit
Dependencies
PygmentsrequiredProvides the core syntax highlighting framework that these lexers extend.
pythonrequiredRequires Python 3.8 or newer to run.
Agent activity
8 hits · last 30 days
node
2
ahrefsbot
2
seranking-bot
2
Amazon
1
Resources