Registry / linecache2

linecache2

JSON →
library1.0.0pypypi✓ verified 23d ago

linecache2 is a Python library providing backports of the standard library's `linecache` module to older supported Python versions (specifically Python 2.6, 3.2, 3.3, and 3.4). It enables random access to individual lines from text files, optimizing internally through a cache, similar to the built-in module. The current version is 1.0.0, released in March 2015, and its development status is 'Mature', indicating stability rather than active feature development.

pip install linecache2
INSTALL
IMPORT
SIG · LINECACHE2
L
linecache2
pythonv1.0.0
Install
1.6s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.002s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

getline
from linecache2 import getline
import linecache; linecache.getline(...)
While 'linecache' is the standard module, 'linecache2' is a distinct backport. Directly importing from 'linecache2' ensures you use the backported version, especially in environments where its specific behavior (e.g., unicode handling in Python 2.x) might be desired or required by dependent libraries like traceback2.
clearcache
from linecache2 import clearcache
import linecache; linecache.clearcache()
As with getline, direct import from linecache2 is necessary to interact with the backported module's cache.

This quickstart demonstrates how to use `linecache2` to retrieve specific lines from a file, similar to the standard `linecache` module. It includes examples of `getline` to fetch a line by its number, `checkcache` to update cache entries, and `clearcache` to remove cached data. The core functionality mirrors the standard library, but with compatibility for older Python versions.

import os from linecache2 import getline, clearcache, checkcache # Create a dummy file for demonstration filename = 'example_file.py' with open(filename, 'w') as f: f.write('print("Hello, world!")\n') f.write('x = 10\n') f.write('y = x + 5\n') f.write('print(f"The value of y is {y}")\n') # Get a specific line from the file line_2 = getline(filename, 2) print(f"Line 2: {line_2.strip()}") # Check if the cache needs updating (e.g., if the file changed on disk) checkcache(filename) # Clear the cache for a specific file or entirely clearcache(filename) # Verify line is empty after clearing cache (unless re-read) # line_2_after_clear = getline(filename, 2) # This would re-read and cache it again # Clean up the dummy file os.remove(filename)
Debug
Known issues
gotchalinecache2 is a backport designed for older Python versions (2.6, 3.2, 3.3, 3.4). Using it in modern Python environments (3.5+) is generally unnecessary as the functionality is built-in, and the native `linecache` module might offer newer features or optimizations not present in the backport (e.g., support for frozen modules in Python 3.14).
fix
For Python versions 3.5 and above, prefer the built-in `linecache` module. Only use `linecache2` if you explicitly target one of its supported older Python versions or if a dependency (like `traceback2`) requires it.
affects: < 3.5
deprecatedThe library's last release was in March 2015, and it primarily targets Python 2.6 and 3.2-3.4. Given the end-of-life for Python 2 and older Python 3 versions, `linecache2` is effectively deprecated for new development.
fix
For new projects, target Python 3.5+ and use the standard `linecache` module. If maintaining legacy code on older Python versions, ensure `linecache2` is correctly installed and its specific behaviors are understood, especially its unicode handling in Python 2.x when used by `traceback2`.
affects: All versions (1.0.0)
gotcha`traceback2`, which also supports older Python versions, depends on `linecache2`. In Python 2.x, `traceback2` creates unicode output, unlike the standard `traceback` module, due to its reliance on `linecache2`. This can lead to different output encoding behaviors in legacy Python 2 applications.
fix
Be aware of the unicode output when using `linecache2` (or libraries dependent on it like `traceback2`) in Python 2.x. Handle string types carefully, explicitly encoding or decoding as needed, or migrate to Python 3 where unicode handling is more consistent.
affects: Python 2.x when used with `traceback2`
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'linecache2'
The linecache2 package is not installed in the current Python environment.
fix
Install the package using pip: `pip install linecache2`
ImportError: No module named linecache
This error often occurs when building executables (e.g., with py2exe) or in environments where the standard `linecache` module (or the `linecache2` backport aliased as `linecache`) is not properly included or found in the Python path. In older Python versions, this could indicate an issue with the standard library's availability or a failed attempt to import `linecache2` as `linecache`.
fix
Ensure `linecache2` is installed and properly packaged with your application. If aliasing, ensure the import statement is `import linecache2 as linecache`. For packaging tools like py2exe, ensure `linecache` (or `linecache2`) is explicitly included in the build configuration.
linecache.getline() returns empty string
The `linecache.getline()` function (and by extension `linecache2`'s `getline`) is designed to return an empty string (`''`) if the specified file is not found, or the requested line number is out of the valid range for the file, rather than raising an exception.
fix
Before calling `linecache.getline()`, verify that the `filename` exists and is accessible, and that `lineno` is within the file's line count. The module also caches file contents, so if the file changes on disk, `linecache.checkcache(filename)` or `linecache.clearcache()` might be needed to refresh the cache.
AttributeError: module 'linecache' has no attribute 'checkcache'
This error typically arises when an outdated or unexpected version of the `linecache` module is being imported, or when a user-defined module or another library's module with the same name is shadowing the standard library's `linecache`, causing a collision and an attempt to access an attribute that doesn't exist in the loaded module.
fix
Inspect your `sys.path` to identify any unexpected module loading order or shadowed `linecache` modules. Rename any custom files named `linecache.py` or `linecache2.py` if they are causing conflicts. Ensure your Python environment and dependencies are correctly set up, especially if you are working with older Python versions where `linecache2` is used to provide `linecache` functionality. You might also encounter similar `AttributeError` for other functions like `lazycache` or `cache` due to version discrepancies or internal usage variations.
Upgrade
Version history
1.0.0latest on PyPI · released Mar 6, 2015
Audit
Dependencies

No dependency data recorded yet.

Agent activity
3 hits · last 30 days
node
2
Resources
linecache2 — pip install linecache2 · libregistry