Pygments
Colored code for pages, docs and terminals.
1. License
“Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met”
Keep the copyright notice and license text with the copies you ship.
github.com/pygments/pygments/blob/master/LICENSE3. Used by agents' projects
Dependency in 61 public GitHub projects set up for AI coding agents that we scanned (1.0%).
4. Agent access
Agent skills for this library in the skills catalog; community skills are written by others, read them before you install.
From the package registry and GitHub, checked daily; card checked Nov 11, 2026. Tap a verdict to see its source.How we checkedReport an error
What Pygments does
Pygments is a free Python library that colors program code the way code editors do. Your Python app or docs site gives it a code snippet and the language, and gets back colored HTML, terminal text or other formats. It knows about 600 languages and file formats.
What people build with it
Code samples on a blog or docs site
A site built in Python shows code blocks in articles with colors and line numbers instead of plain gray text.
Colored output in a command-line tool
A Python script that prints code, logs or config files shows them in color in the terminal.
Code examples in project documentation
Documentation built with Sphinx or Material for MkDocs colors its code examples with Pygments.
Code in printed reports and papers
A report generator puts colored code into LaTeX or RTF files for PDFs and word processors, not only web pages.
Code in answers from an AI chat Idea
A Python chat app colors the code blocks in model answers on the server, with a time limit for each snippet.
Code pictures for social posts Idea
Turn a short snippet into a PNG to post where code blocks are not supported; this needs the Pillow library too.
Pygments popularity
Gid Score measures public popularity and activity, not quality. How the Gid Score is calculated
How Pygments is used
- Pygments is a dependency in 61 of the 6,258 public GitHub projects set up for AI coding agents that we scanned (3.8% of the 1,623 Python ones), #11 of 149 in markdown, editors and content.
- Counting the projects our scan samples by dependency (Solana 3,067, Ethereum and EVM 1,774, 3D and games 3,130, agent frameworks and SDKs 13,750), it appears in 1,109; those are left out of every share on this page.
- Projects that use it most often also use Pydantic (78.7%), Click (77.0%), Requests (73.8%).
By coding agent
Often used with
More about Pygments
More in Markdown, editors and content
Alternatives
Pygments This page
Pick Shiki for JavaScript sites that want code colored with the same grammars and themes as VS Code.
Pick highlight.js to color code in the browser, with automatic language detection.
Pick PrismJS for a small in-browser highlighter with plugins for line numbers and copy buttons.
Get started
Tell your AI assistant
“Use the Python library Pygments (pin Pygments==2.21.0, Python 3.9+) to turn code blocks into highlighted HTML with highlight(code, get_lexer_by_name(lang), HtmlFormatter()), fall back to TextLexer for unknown languages, and add the CSS from HtmlFormatter().get_style_defs('.highlight').”
Install Pygments into your project's Python environment; it needs nothing else.
pip install PygmentsFrom pygments.org/faq/
Pick a lexer for the language and a formatter for the output, then call highlight().
from pygments import highlight from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter code = 'print "Hello World"' print(highlight(code, PythonLexer(), HtmlFormatter()))Make the stylesheet once and add it to your page, or the HTML stays uncolored.
print(HtmlFormatter().get_style_defs('.highlight'))
Mistakes to avoid
By default the HTML only carries short class names like k and s; the colors live in the stylesheet.
Guessing scores every lexer on the text; in our run a four-line SQL query was guessed as scdoc.
Some inputs make Pygments run for a very long time or use a lot of memory, which can take the server down.
The image formatters draw with the Python Imaging Library, which Pygments does not install.
Questions
Why is my highlighted HTML still black and white?
The HTML uses class names; you also need the stylesheet from get_style_defs(), or noclasses=True for inline colors.
Can it figure out the language by itself?
It can guess from a file name or the text, but guessing short snippets is unreliable; pass the language when you know it.
Can I use it from the command line?
Yes. The pygmentize command colors a file in the terminal or writes an HTML file.
My security scanner flags a Pygments advisory. Am I at risk?
The project says the risk is slow processing of untrusted input in a service; a docs build that does not take user input is not exposed.
Which Python version does it need?
Pygments 2.21.0 needs Python 3.9 or newer.