On this page
linecache
Prev: keyword | Up: stdlib_module | Next: locale
Rel: pathlib, os, io, time, fileinput
Ref: docs.python.org — linecache
Tags: #public #python3 #stdlib
Import
import linecache
Overview
In-memory line cache for Python source files (getline(), getlines(), checkcache(), clearcache()), used internally by traceback, pdb, and warnings to display source snippets quickly. Use linecache when building custom diagnostics, error reporters, or template debuggers that repeatedly look up specific line numbers.
Quick Start
import linecache
# Read line 1 of this module's own source file (1-indexed)
first_line = linecache.getline(linecache.__file__, 1)
assert isinstance(first_line, str) and len(first_line) > 0
Implementation Walkthrough
1. Pulling a Context Window Around an Error Line
getline(filename, lineno) safely returns "" (never raises IndexError or FileNotFoundError) when a file is missing or lineno is out of bounds:
import linecache
def source_excerpt(filename: str, lineno: int, radius: int = 2) -> list[tuple[int, str]]:
start = max(1, lineno - radius)
return [
(n, line.rstrip("\n"))
for n in range(start, lineno + radius + 1)
if (line := linecache.getline(filename, n))
]
Gotchas & Boundaries
- 1-based line numbers: Line
1is the first line of the file;getline(path, 0)returns"". - Cached until invalidated: If a file changes on disk,
linecachekeeps serving the old contents until you calllinecache.checkcache(filename)orlinecache.clearcache(). - Watch memory on huge files:
linecachereads and caches the entire file in RAM on first access—don't point it at multi-gigabyte data logs.
Common Functions
- linecache.checkcache() —
checkcache(filename=None)— Discard cache entries that are out of date. - linecache.clearcache() —
clearcache()— Clear the cache entirely. - linecache.getline() —
getline(filename, lineno, module_globals=None)— Get a line for a Python source file from the cache. - linecache.lazycache() —
lazycache(filename, module_globals)— Seed the cache for filename with module_globals.
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/keyword
- tuts/python3/stdlib_module/linecache/checkcache
- tuts/python3/stdlib_module/linecache/clearcache
- tuts/python3/stdlib_module/linecache/getline
- tuts/python3/stdlib_module/linecache/lazycache
- tuts/python3/stdlib_module/locale
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/keyword Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/linecache/checkcache Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/linecache/clearcache Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/linecache/getline Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/linecache/lazycache Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/locale Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/fileinput Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; shared tags: #stdlib