{"id":584,"library":"termcolor","title":"ANSI Terminal Color Formatting","description":"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.","status":"active","version":"3.3.0","language":"python","source_language":"en","source_url":"https://github.com/termcolor/termcolor","tags":["terminal","color","formatting","ansi","cli","output"],"install":[{"cmd":"pip install termcolor","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"colored","correct":"from termcolor import colored"},{"symbol":"cprint","correct":"from termcolor import cprint"}],"quickstart":{"code":"import sys\nfrom termcolor import colored, cprint\nimport os\n\n# Basic colored text\nprint(colored(\"Hello, World!\", \"red\"))\n\n# Colored text with background and attributes\ncprint(\"Hello, colorful world!\", \"green\", \"on_yellow\", attrs=[\"bold\", \"underline\"])\n\n# True color (RGB) example (requires a compatible terminal and termcolor >= 3.1.0)\n# You can also specify 0-255 RGB ints via a tuple for color and on_color\nif os.environ.get('FORCE_COLOR', '0') == '1': # Check for forced color for consistent demo\n    cprint(\"True color text!\", (100, 150, 250), (50, 60, 70), attrs=[\"italic\"])\nelse:\n    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\"]))\n\n# Print to stderr\ncprint(\"This is an error message.\", \"red\", attrs=[\"reverse\"], file=sys.stderr)","lang":"python","description":"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`."},"warnings":[{"fix":"Ensure your project runs on Python 3.10 or newer, or pin `termcolor` to an older version compatible with your Python environment.","message":"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.","severity":"breaking","affected_versions":"< 3.10"},{"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.","message":"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.","severity":"gotcha","affected_versions":"< 3.0.0"},{"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.","message":"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.","severity":"gotcha","affected_versions":"< 3.1.0 (RGB), < 3.3.0 (italic)"}],"env_vars":null,"last_verified":"2026-05-12T16:20:52.721Z","next_check":"2026-06-28T00:00:00.000Z","problems":[{"fix":"Install the library using pip: `pip install termcolor`","cause":"The 'termcolor' library is not installed in the current Python environment or the environment is not activated.","error":"ModuleNotFoundError: No module named 'termcolor'"},{"fix":"Use one of the supported color names such as 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', 'grey', 'black'.","cause":"The 'colored' function was called with a color name that is not one of the 16 supported basic color names by termcolor.","error":"ValueError: Invalid color name 'purple'"},{"fix":"Use the 'colored()' function and pass the color name as a string argument, e.g., `colored('text', 'red')`.","cause":"The user is attempting to access a color directly as an attribute of the 'termcolor' module instead of using the 'colored()' function.","error":"AttributeError: module 'termcolor' has no attribute 'red'"},{"fix":"Use the 'on_color' keyword argument for background colors, e.g., `colored('text', 'white', 'on_red')`.","cause":"The 'colored' function was called with an incorrect keyword argument for specifying the background color; it expects 'on_color'.","error":"TypeError: colored() got an unexpected keyword argument 'bg'"},{"fix":"Correct the import statement to `from termcolor import colored`.","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.","error":"ModuleNotFoundError: No module named 'termcolor.color'"}],"ecosystem":"pypi","meta_description":null,"install_score":80,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":"3.3.0","install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"17.8M"},{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"17.8M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0,"mem_mb":0,"disk_size":"18M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"19.6M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"19.6M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.5,"import_time_s":0,"mem_mb":0,"disk_size":"20M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"11.5M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"11.5M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":0,"mem_mb":0,"disk_size":"12M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0.2,"disk_size":"11.3M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0.2,"disk_size":"11.2M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.4,"import_time_s":0,"mem_mb":0,"disk_size":"12M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"17.3M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"17.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":" $EXIT -eq 0 ","exit_code":0,"wheel_type":"wheel","failure_reason":null,"install_time_s":1.7,"import_time_s":0,"mem_mb":0,"disk_size":"18M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0,"mem_mb":0,"disk_size":"18M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}