Typing stubs for termcolor
This is a PEP 561 type stub package for the 'termcolor' library, providing external type annotations for static analysis tools like mypy, pyright, and PyCharm. The current version is 1.1.6.2, released on March 27, 2023. These stub packages are automatically generated and released by the typeshed internal machinery, typically on a daily cadence, ensuring up-to-date type information for various Python libraries.
Warnings
- breaking If 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.
- gotcha The `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.
- 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.
- gotcha Typeshed 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.
Install
-
pip install types-termcolor
Imports
- colored
from termcolor import colored
- cprint
from termcolor import cprint
Quickstart
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)