ANSI Terminal Color Formatting

raw JSON →
3.3.0 verified Tue May 12 auth: no python install: verified quickstart: verified

Termcolor is a Python library that provides ANSI color formatting for output in the terminal. It supports 16 colors, 256 colors, and 24-bit true color modes, along with various text attributes like bold, underline, and italic. It is actively maintained with frequent updates, currently at version 3.3.0.

pip install termcolor
error ModuleNotFoundError: No module named 'termcolor'
cause The 'termcolor' library is not installed in the current Python environment or the environment is not activated.
fix
Install the library using pip: pip install termcolor
error ValueError: Invalid color name 'purple'
cause The 'colored' function was called with a color name that is not one of the 16 supported basic color names by termcolor.
fix
Use one of the supported color names such as 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'grey', 'black'.
error AttributeError: module 'termcolor' has no attribute 'red'
cause The user is attempting to access a color directly as an attribute of the 'termcolor' module instead of using the 'colored()' function.
fix
Use the 'colored()' function and pass the color name as a string argument, e.g., colored('text', 'red').
error TypeError: colored() got an unexpected keyword argument 'bg'
cause The 'colored' function was called with an incorrect keyword argument for specifying the background color; it expects 'on_color'.
fix
Use the 'on_color' keyword argument for background colors, e.g., colored('text', 'white', 'on_red').
error ModuleNotFoundError: No module named 'termcolor.color'
cause The user is attempting an incorrect import path, assuming a submodule for colors, whereas the 'colored' function is directly available from the 'termcolor' module.
fix
Correct the import statement to from termcolor import colored.
breaking Termcolor has incrementally dropped support for older Python versions. Version 3.2.0 dropped support for Python 3.9, version 2.5.0 dropped Python 3.8, and version 2.4.0 dropped Python 3.7. The current minimum required Python version is 3.10.
fix Ensure your project runs on Python 3.10 or newer, or pin `termcolor` to an older version compatible with your Python environment.
gotcha The handling of `FORCE_COLOR`, `NO_COLOR`, and `ANSI_COLORS_DISABLED` environment variables changed in version 3.0.0. Prior to 3.0.0, empty string values for these variables might have been interpreted differently. As of 3.0.0, these variables (and `no_color`/`force_color` function parameters) are only applied when present and contain a non-empty string.
fix Ensure environment variables like `NO_COLOR` are set to a non-empty string (e.g., '1' or 'true') to properly disable coloring, and be aware of the precedence order where function parameters override environment variables.
gotcha New text attributes and color modes were introduced in recent versions. The `italic` attribute was added in 3.3.0. True color (RGB tuple) support was added in version 3.1.0. Attempting to use these features with older `termcolor` versions will result in errors or lack of intended formatting.
fix Upgrade to `termcolor` 3.1.0 or newer for true color support, and 3.3.0 or newer for the `italic` attribute. Ensure your terminal emulator also supports these features.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.00s 17.8M
3.10 alpine (musl) - - 0.00s 17.8M
3.10 slim (glibc) wheel 1.5s 0.00s 18M
3.10 slim (glibc) - - 0.00s 18M
3.11 alpine (musl) wheel - 0.00s 19.6M
3.11 alpine (musl) - - 0.00s 19.6M
3.11 slim (glibc) wheel 1.5s 0.00s 20M
3.11 slim (glibc) - - 0.00s 20M
3.12 alpine (musl) wheel - 0.00s 11.5M
3.12 alpine (musl) - - 0.00s 11.5M
3.12 slim (glibc) wheel 1.4s 0.00s 12M
3.12 slim (glibc) - - 0.00s 12M
3.13 alpine (musl) wheel - 0.00s 11.3M
3.13 alpine (musl) - - 0.00s 11.2M
3.13 slim (glibc) wheel 1.4s 0.00s 12M
3.13 slim (glibc) - - 0.00s 12M
3.9 alpine (musl) wheel - 0.00s 17.3M
3.9 alpine (musl) - - 0.00s 17.3M
3.9 slim (glibc) wheel 1.7s 0.00s 18M
3.9 slim (glibc) - - 0.00s 18M

Demonstrates basic text coloring, background colors, text attributes (bold, underline, italic), and true color (RGB) support. It also shows how to print to `sys.stderr`.

import sys
from termcolor import colored, cprint
import os

# Basic colored text
print(colored("Hello, World!", "red"))

# Colored text with background and attributes
cprint("Hello, colorful world!", "green", "on_yellow", attrs=["bold", "underline"])

# True color (RGB) example (requires a compatible terminal and termcolor >= 3.1.0)
# You can also specify 0-255 RGB ints via a tuple for color and on_color
if os.environ.get('FORCE_COLOR', '0') == '1': # Check for forced color for consistent demo
    cprint("True color text!", (100, 150, 250), (50, 60, 70), attrs=["italic"])
else:
    print(colored("Italic attribute and true color (RGB) requires termcolor >= 3.3.0 and >= 3.1.0 respectively, and a compatible terminal.", "white", attrs=["dark"]))

# Print to stderr
cprint("This is an error message.", "red", attrs=["reverse"], file=sys.stderr)