---
source: https://iinventedlight.com/library/notes/python-tokenize.md
title: "Python tokenize: a lexical scanner for source code"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, tokenize — Tokenizer for Python source; CPython, tokenize documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-tokenize"
tags: [python, tokenize, lexer, tokens, encoding]
client: Librarian
sources:
  - id: S1
    title: "tokenize — Tokenizer for Python source"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/tokenize.html
    accessed: 2026-11-23
    license: "PSF License Version 2"
  - id: S2
    title: "tokenize documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/tokenize.rst
    accessed: 2026-11-23
    license: "PSF License Version 2"
---
# Python tokenize: a lexical scanner for source code
**Answer:** The tokenize module provides a lexical scanner for Python source code, written in Python. That scanner returns comments as tokens as well. detect_encoding will call readline a maximum of twice.

## A lexical scanner for source

The tokenize module provides a lexical scanner for Python source code, written in Python.

Comments are returned as tokens as well, which is useful for pretty-printers.

Operator and delimiter tokens, and also Ellipsis, come back as the generic OP token type.

The exact_type property on TokenInfo gives the exact token type.

## Encoding detection and valid input

detect_encoding detects the encoding for decoding a Python source file.

The function will call readline a maximum of twice, then return the encoding used.

Functions in this module are only designed to parse syntactically valid Python code.

When providing invalid Python code, the behavior is undefined and it can change at any point.

## See also

- [python-ast](https://iinventedlight.com/library/notes/python-ast/)
- [python-dis](https://iinventedlight.com/library/notes/python-dis/)
- [python-inspect](https://iinventedlight.com/library/notes/python-inspect/)

## Claims

- id: C1
  claim: The module is a lexical scanner for Python source, written in Python.
  source: S1
  evidence: "lexical scanner for Python source code, implemented in Python"

- id: C2
  claim: The scanner also returns comments as tokens.
  source: S1
  evidence: "returns comments as tokens as well"

- id: C3
  claim: detect_encoding calls readline at most twice.
  source: S2
  evidence: "It will call readline a maximum of twice"

- id: C4
  claim: The helpers are designed for syntactically valid Python code.
  source: S2
  evidence: "only designed to parse syntactically valid Python code"

## Sources

- [S1] tokenize — Tokenizer for Python source. Python Software Foundation. https://docs.python.org/3/library/tokenize.html . License: PSF License Version 2. Accessed 2026-11-23.

- [S2] tokenize documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/tokenize.rst . License: PSF License Version 2. Accessed 2026-11-23.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, tokenize — Tokenizer for Python source; CPython, tokenize documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
