linecache: read any line from a text file through a cache
Answer: The linecache module allows one to get any line from a Python source file and uses a cache. This is used by the traceback module to retrieve source lines. getline will never raise an exception.
Why the cache exists
The module gets any line from a Python source file and uses a cache for the common case where many lines are read from a single file.
The traceback module depends on this to retrieve source lines for a formatted traceback.
Files are opened with tokenize.open(), and tokenize.detect_encoding() picks the file encoding, which defaults to UTF-8 in the absence of an encoding token.
getline and the cache helpers
Call getline(filename, lineno, module_globals=None) to get line lineno from the named file.
This function will never raise an exception. On errors it returns '', and a successful line still includes its terminating newline.
clearcache() drops cached lines when you no longer need them. checkcache() revalidates entries if files may have changed on disk.
The same contract in the source docs
The library source documentation also says this is used by the traceback module to retrieve source lines.
See also
Claims
- id: C1 claim: linecache gets any line from a Python source file and caches repeated reads. source: S1 evidence: "get any line from a Python source file"
- id: C2 claim: traceback uses linecache to retrieve source lines. source: S1 evidence: "used by the traceback module to retrieve source lines"
- id: C3 claim: getline never raises; it returns an empty string on errors. source: S1 evidence: "This function will never raise an exception"
- id: C4 claim: The source docs also describe the cache for many lines from one file. source: S2 evidence: "many lines are read from a single file"
- id: C5 claim: The source docs also say traceback retrieves source lines through this module. source: S2 evidence: "retrieve source lines for inclusion in the formatted traceback"
Sources
- [S1] linecache — Random access to text lines. Python Software Foundation. https://docs.python.org/3/library/linecache.html . License: PSF License Version 2. Accessed 2026-11-23.
- [S2] linecache module documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/linecache.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, linecache — Random access to text lines; CPython, linecache module documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, linecache — Random access to text lines; CPython, linecache module documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "linecache: read any line from a text file through a cache." iinventedlight Library, iil:note:qv65vhjd, version of 2026-11-23. https://iinventedlight.com/library/notes/python-linecache/. License: CC-BY-4.0.
@misc{iil_note_qv65vhjd,
author = {{iinventedlight}},
title = {{linecache: read any line from a text file through a cache}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-23},
url = {https://iinventedlight.com/library/notes/python-linecache/},
note = {Permanent ID iil:note:qv65vhjd. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-linecache.schema.json (schema; CC-BY-4.0)
- python-linecache.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-linecache.md.