Install & Compatibility
Where this runs
tested against v1.1.6.2 · 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
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 1.5s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
colored
✓ from termcolor_stubs import colored
✗ from termcolor-stubs import colored
This quickstart demonstrates how to use the `colored` function and `cprint` from the `termcolor` library to output text with foreground colors, background highlights, and text attributes to the terminal. When `types-termcolor` is installed, it enables type checkers to provide hints and validate the usage of these functions, improving code quality and readability.
from termcolor import colored, cprint
import sys
# Basic usage of 'colored'
print(colored('Hello, World!', 'red'))
# Using 'colored' with background color and multiple attributes
text_with_attributes = colored(
"Hello, beautifully colored World!",
"green",
"on_red",
attrs=["bold", "underline"]
)
print(text_with_attributes)
# Using 'cprint' for direct printing
cprint("This is a blue and bold message.", "blue", attrs=["bold"])
# Example with error handling, printing to stderr in red
try:
raise ValueError("Something went wrong during processing!")
except ValueError as e:
cprint(f"Error: {e}", "white", "on_red", attrs=["bold"], file=sys.stderr)
Debug
Known issues
breakingIf you are using `termcolor` version 2.0.0 or newer, you should uninstall `types-termcolor`. As of version 2.0.0, the `termcolor` package includes its own inline type annotations, making this stub package redundant and potentially causing conflicts or issues with type checkers.fixUninstall `types-termcolor` by running `pip uninstall types-termcolor`. Ensure your type checker is configured to use the inline types from `termcolor`.
affects: termcolor>=2.0.0
gotchaThe `types-termcolor` package provides *only* type stubs; it does not contain the runtime code for the `termcolor` library itself. Your program will not execute correctly without the actual `termcolor` library installed.fixEnsure the `termcolor` library is installed alongside `types-termcolor` by running `pip install termcolor`.
affects: all
gotcha`termcolor` relies on ANSI escape codes to produce colored output in the terminal. Not all terminals (e.g., older Windows Command Prompt or when piping output to certain tools) support these codes, which can result in raw escape sequences being printed instead of the intended colored text.fixUse a modern terminal emulator (e.g., Windows Terminal, most Linux/macOS terminals, VS Code integrated terminal). When piping output, some tools like `less` may require a `-r` flag to interpret raw ANSI escape sequences (e.g., `python script.py | less -r`).
affects: all
gotchaTypeshed stub packages, including `types-termcolor`, use a versioning scheme where parts of the version number correlate to the runtime package's version. While this aims for compatibility, aggressively pinning stub package versions might lead to incompatibilities if the runtime `termcolor` package updates independently, or cause you to miss valuable type stub improvements.fixConsider using flexible version specifiers for stub packages (e.g., `types-termcolor>=1.1,<1.2` if `termcolor>=1.1,<1.2` is used). Regularly review and update pinned stub versions to leverage improvements and maintain compatibility.
affects: all
Upgrade
Version history
1.1.6.2latest on PyPI · released Mar 28, 2023
Audit
Dependencies
termcolorrequired`types-termcolor` provides type stubs for the `termcolor` package; it does not offer runtime functionality itself. It is only beneficial if the `termcolor` library is also installed in your environment.