# Pygments — card for AI agents

Syntax highlighting library for code in Python supporting hundreds of programming languages.

Category: Markdown, editors and content · PyPI package `pygments` · latest version 2.21.0 (checked 2026-11-01)
Install: `pip install pygments`
License: BSD-2-Clause (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Numbers (AgentGid data)
- Gid Score: 71.1 (#337 of all tools, 17 of 149 in its category)
- Used in 61 public projects set up for AI coding agents (1.0%)
- Downloads last week: 287,018,363 (as of 2026-11-24), +12.7% over 4 weeks
- GitHub stars: 2,226 (as of 2026-11-24)
- Last push: 2026-09-27

## Often used with
Pydantic 78.7%, Click 77.0%, Requests 73.8%, packaging 72.1%, PyYAML 72.1%

## Links
- AgentGid page: https://agentgid.com/tools/pygments/
- Homepage: https://pygments.org
- Repository: https://github.com/pygments/pygments


## Pygments: colored code for pages, docs and terminals

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.

### Where to use 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: A Python chat app colors the code blocks in model answers on the server, with a time limit for each snippet. (idea, not from the official pages)
- Code pictures for social posts: Turn a short snippet into a PNG to post where code blocks are not supported; this needs the Pillow library too. (idea, not from the official pages)

### How to start
- Install Pygments into your project's Python environment; it needs nothing else.

```
pip install Pygments
```

- 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'))
```


### License
Free to use, change and ship in paid products under the BSD 2-clause license. Keep the copyright notice and license text with copies you distribute.

### Good to know
- It has no time limit. Some unlucky input can make it run for a very long time, so code typed by strangers needs a timeout.
- It only colors code. It does not check code for mistakes or understand what the code does.
- It runs in Python. A site that colors code in the browser uses a JavaScript highlighter instead.
- The project calls itself stable and slow to change; version 2.21.0 came out on August 17, 2026.

Checked 2026-11-11 against: https://pygments.org/, https://pygments.org/faq/, https://pygments.org/docs/quickstart/, https://pygments.org/docs/security/, https://pygments.org/docs/formatters/, https://pygments.org/docs/changelog/, https://github.com/pygments/pygments/blob/master/README.rst, https://github.com/pygments/pygments/blob/master/LICENSE, https://pypi.org/pypi/pygments/json

### Mistakes to avoid
- Don't: Inserting highlight(...) output into a page without any stylesheet → Do: Add HtmlFormatter(style=...).get_style_defs('.highlight') to your CSS, or use HtmlFormatter(noclasses=True) for HTML email (By default the HTML only carries short class names like k and s; the colors live in the stylesheet.)
- Don't: lexer = guess_lexer(snippet) for short chat or markdown code blocks → Do: Use the language tag from the code fence with get_lexer_by_name(), and fall back to TextLexer on ClassNotFound (Guessing scores every lexer on the text; in our run a four-line SQL query was guessed as scdoc.)
- Don't: Highlighting code that visitors paste, inside the web request, with no limit → Do: Cap the input size, run highlighting in a separate process with a timeout, and limit how many run at once (Some inputs make Pygments run for a very long time or use a lot of memory, which can take the server down.)
- Don't: Using ImageFormatter (PNG or JPG output) with only Pygments installed → Do: Install Pillow as well before using the image formatters (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.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
