{"id":4083,"library":"linecache2","title":"linecache2","description":"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.","status":"maintenance","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/testing-cabal/linecache2","tags":["backport","linecache","python2","python3","legacy"],"install":[{"cmd":"pip install linecache2","lang":"bash","label":"Install linecache2"}],"dependencies":[],"imports":[{"note":"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.","wrong":"import linecache; linecache.getline(...)","symbol":"getline","correct":"from linecache2 import getline"},{"note":"As with getline, direct import from linecache2 is necessary to interact with the backported module's cache.","wrong":"import linecache; linecache.clearcache()","symbol":"clearcache","correct":"from linecache2 import clearcache"}],"quickstart":{"code":"import os\nfrom linecache2 import getline, clearcache, checkcache\n\n# Create a dummy file for demonstration\nfilename = 'example_file.py'\nwith open(filename, 'w') as f:\n    f.write('print(\"Hello, world!\")\\n')\n    f.write('x = 10\\n')\n    f.write('y = x + 5\\n')\n    f.write('print(f\"The value of y is {y}\")\\n')\n\n# Get a specific line from the file\nline_2 = getline(filename, 2)\nprint(f\"Line 2: {line_2.strip()}\")\n\n# Check if the cache needs updating (e.g., if the file changed on disk)\ncheckcache(filename)\n\n# Clear the cache for a specific file or entirely\nclearcache(filename)\n\n# Verify line is empty after clearing cache (unless re-read)\n# line_2_after_clear = getline(filename, 2) # This would re-read and cache it again\n\n# Clean up the dummy file\nos.remove(filename)","lang":"python","description":"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."},"warnings":[{"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.","message":"linecache2 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).","severity":"gotcha","affected_versions":"< 3.5"},{"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`.","message":"The 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.","severity":"deprecated","affected_versions":"All versions (1.0.0)"},{"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.","message":"`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.","severity":"gotcha","affected_versions":"Python 2.x when used with `traceback2`"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}