Pygments
Pygments is a generic syntax highlighting library written in Python, supporting over 500 languages and text formats with output in HTML, LaTeX, RTF, SVG, image formats, and ANSI terminal sequences. It can be used both as a library and as the `pygmentize` CLI tool. Current version is 2.19.2. Releases are made periodically with new lexers added each minor version; patch releases fix regressions quickly (2.19.1 and 2.19.2 followed 2.19.0 within weeks).
Warnings
- gotcha HtmlFormatter output does NOT include CSS by default. The generated HTML uses CSS classes but the stylesheet must be obtained separately via `formatter.get_style_defs('.highlight')` and injected into the page. Without it the output appears unstyled.
- breaking Python 3.7 and below are no longer supported as of Pygments 2.18.0. The `importlib-metadata` backport is no longer required or used. The `pip install Pygments[plugins]` extra is a no-op.
- gotcha All lexer/formatter/style lookup functions (`get_lexer_by_name`, `get_lexer_for_filename`, `get_formatter_by_name`, etc.) raise `pygments.util.ClassNotFound` — not a built-in like `KeyError` or `ValueError` — when no match is found. Uncaught, this crashes silently in many frameworks.
- gotcha Pygments provides no execution-time guarantees. Certain inputs (especially adversarial or malformed code) can trigger catastrophic backtracking in lexer regexes, causing the process to hang or consume excessive memory. This is a known DoS vector for web services.
- gotcha `get_lexer_for_filename()` only checks the primary (unique) filename list and can raise `ClassNotFound` for ambiguous extensions like `.html`. `guess_lexer_for_filename()` also checks secondary patterns and runs content analysis, but is slower.
- gotcha The `cssclass` option on `HtmlFormatter` must match the selector prefix passed to `get_style_defs()`. If you set `cssclass='source'` but call `get_style_defs('.highlight')`, the CSS will not apply to the generated markup.
- deprecated `STYLE_MAP` in `pygments.styles` uses an older format and does not include plugin styles. It is kept for backwards compatibility but is incomplete.
Install
-
pip install Pygments -
pip install Pygments[windows-terminal]
Imports
- highlight
from pygments import highlight
- PythonLexer (and other named lexers)
from pygments.lexers import PythonLexer
- get_lexer_by_name
from pygments.lexers import get_lexer_by_name
- get_lexer_for_filename
from pygments.lexers import get_lexer_for_filename
- guess_lexer
from pygments.lexers import guess_lexer
- HtmlFormatter
from pygments.formatters import HtmlFormatter
- ClassNotFound
from pygments.util import ClassNotFound
- get_all_styles
from pygments.styles import get_all_styles
- RegexLexer
from pygments.lexer import RegexLexer
Quickstart
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound
code = '''
def greet(name: str) -> str:
return f"Hello, {name}!"
print(greet("World"))
'''
try:
lexer = get_lexer_by_name('python', stripall=True)
except ClassNotFound as e:
raise SystemExit(f"Lexer not found: {e}")
# cssclass must match the selector passed to get_style_defs
formatter = HtmlFormatter(linenos=True, cssclass='highlight', style='default')
# highlighted is an HTML snippet — NOT a full document
highlighted = highlight(code, lexer, formatter)
# get_style_defs() must be called to obtain the CSS; it is NOT embedded by default
css = formatter.get_style_defs('.highlight')
print(f'<style>\n{css}\n</style>')
print(highlighted)