backports.shutil_get_terminal_size
This library provides a pure Python backport of the `get_terminal_size` function, originally introduced in Python 3.3's `shutil` module. It allows applications targeting older Python versions (like Python 2.6, 2.7, and 3.2) to access terminal dimension information. Currently at version 1.0.0, the package was last updated in 2014, indicating a stable, low-cadence release cycle typical for a compatibility backport.
Warnings
- gotcha This backport is primarily intended for Python versions older than 3.3. If you are using Python 3.3 or newer, `shutil.get_terminal_size` is built-in and should be used directly, making this package redundant. Installing it on newer Python versions will not cause issues but serves no practical purpose.
- gotcha The backport is written in pure Python, unlike the native `shutil.get_terminal_size` in Python 3.3+ which has a C implementation. This means the backported version might be marginally slower in performance, though for most applications, the difference is negligible.
- gotcha Users have reported `ImportError` issues, particularly in mixed installation environments (e.g., pip and conda) or when other `backports` packages conflict. Sometimes the module is installed but not found correctly, or other `backports` packages interfere with the namespace.
Install
-
pip install backports-shutil-get-terminal-size
Imports
- get_terminal_size
from shutil_get_terminal_size import get_terminal_size
from backports.shutil_get_terminal_size import get_terminal_size
Quickstart
from backports.shutil_get_terminal_size import get_terminal_size
# Get terminal dimensions
terminal_size = get_terminal_size()
print(f"Terminal columns: {terminal_size.columns}")
print(f"Terminal lines: {terminal_size.lines}")