{"id":3944,"library":"crayons","title":"crayons","description":"Crayons is a simple Python module that provides colored strings for terminal usage, automatically wrapping a given string in both the foreground color and resetting to the original state after the string is complete. It aims to simplify terminal coloring by managing ANSI escape codes for the user. The current version is 0.4.0, released in August 2020, indicating a stable but less frequently updated library.","status":"active","version":"0.4.0","language":"python","source_language":"en","source_url":"https://github.com/MasterOdin/crayons","tags":["textui","colors","cli","terminal"],"install":[{"cmd":"pip install crayons","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides cross-platform colored terminal text, powering crayons' functionality.","package":"colorama","optional":false}],"imports":[{"note":"The library is imported as 'crayons', and color functions are accessed as attributes, e.g., `crayons.red()`.","symbol":"crayons","correct":"import crayons"}],"quickstart":{"code":"import crayons\nimport os\n\n# Basic color usage\nprint(crayons.red(\"This text is red.\"))\nprint(crayons.green(\"This text is green and bold.\", bold=True))\nprint(crayons.blue(\"This is blue text.\"))\n\n# Using 'always=True' to force color even when not in a TTY\nprint(crayons.yellow(\"This warning should always be yellow.\", always=True))\n\n# Demonstrating the clean function\ncolored_text = crayons.magenta(\"Hello, colorful world!\")\nprint(f\"Original: {colored_text}\")\nprint(f\"Cleaned: {crayons.clean(colored_text)}\")\n\n# Force color using environment variable (useful for CI/CD or non-TTY environments)\n# os.environ['CLINT_FORCE_COLOR'] = '1'\n# print(crayons.cyan(\"This should be cyan even if not in a TTY due to env var.\"))\n# del os.environ['CLINT_FORCE_COLOR'] # Clean up environment variable for subsequent tests","lang":"python","description":"Import the `crayons` library and call color functions (e.g., `crayons.red()`) with your string. You can pass `bold=True` for bold text or `always=True` to force color output even when not in a TTY. The `clean()` function removes ANSI escape codes from a colored string."},"warnings":[{"fix":"If you relied on `crayons.clean()` to strip all non-alphanumeric characters, its behavior changed. Test your code with v0.3.0+ to ensure the new behavior (ANSI code stripping only) is what you expect. If not, implement your own string cleaning logic.","message":"The `clean` function was completely rewritten in v0.3.0. Previously, it might have stripped all characters, but now it specifically removes ANSI escape codes.","severity":"breaking","affected_versions":"<0.3.0"},{"fix":"To force color output regardless of the TTY status, set the `CLINT_FORCE_COLOR` environment variable to any non-empty value (e.g., `export CLINT_FORCE_COLOR=1` or `os.environ['CLINT_FORCE_COLOR'] = '1'` in Python code). Alternatively, use `always=True` on individual color calls.","message":"As of v0.4.0, `crayons` explicitly checks if `sys.stdout` has an `isatty` attribute and if it's `True` before displaying colors. If using a custom stdout object without `isatty` or if `isatty` is `False` (e.g., in a piped environment), colors might not display.","severity":"gotcha","affected_versions":">=0.4.0"},{"fix":"Ensure all inputs to `crayons` color functions are strings. Version 0.3.1 and later explicitly convert incoming parameters to strings, but it's good practice to provide strings yourself.","message":"Prior to v0.3.1, passing non-string parameters directly to `crayons` functions could lead to unexpected behavior (e.g., issues with `len`).","severity":"gotcha","affected_versions":"<0.3.1"}],"env_vars":null,"search_vec":"'0.4.0':54 '2020':58 'aim':37 'ansi':44 'august':57 'automat':15 'cli':69 'code':46 'color':10,24,41,68 'complet':35 'crayon':1,2 'current':51 'escap':45 'foreground':23 'frequent':64 'given':18 'indic':59 'less':63 'librari':66 'manag':43 'modul':7 'origin':29 'provid':9 'python':6 'releas':55 'reset':26 'simpl':5 'simplifi':39 'stabl':61 'state':30 'string':11,19,33 'termin':13,40,70 'textui':67 'updat':65 'usag':14 'user':49 'version':52 'wrap':16","created_at":"2026-04-12T03:34:19.740505+00:00","updated_at":"2026-04-16T04:02:08.153358+00:00","problems":[{"fix":"Ensure your Python script is run directly in an interactive terminal. If you explicitly want colors when piping or redirecting output, you might need to use `crayons.always = True` (though this is not explicitly documented for version 0.4.0, it's a common pattern in similar libraries or earlier/later versions of `crayons` for forcing output). For version 0.4.0, the documentation states 'If the current process is not in a TTY (e.g. being piped), no colors will be displayed', implying this behavior is by design and not directly overridable by a simple flag in this specific version.","cause":"The `crayons` library is designed to apply ANSI escape codes for terminal colors. If the output is being redirected or piped (e.g., to a file, another command, or a non-interactive environment), it detects that it's not running in a TTY and suppresses color output to avoid embedding escape codes in the non-terminal output.","error":"No colors will be displayed if the current process is not in a TTY"},{"fix":"Use one of the supported color attributes, e.g., `crayons.magenta('Hello, World!')`.","cause":"The `crayons` library (version 0.4.0) only supports a specific set of predefined colors: `red`, `green`, `yellow`, `blue`, `black`, `magenta`, `cyan`, `white`, and `normal`. Trying to access a color not in this list will raise an `AttributeError`.","error":"AttributeError: module 'crayons' has no attribute 'purple'"},{"fix":"The color attributes in `crayons` are directly callable. Ensure you are calling the color function with the string argument, e.g., `print(crayons.red('This is red'))`. If you store the colored string, it becomes a `ColoredString` instance and is no longer callable as a function, e.g., `my_red_text = crayons.red('Hello'); print(my_red_text)` is correct, but `print(my_red_text('World'))` would cause this error.","cause":"This error occurs when a developer incorrectly tries to call a color attribute (which is a `ColoredString` object after the first call) as a function, typically after it has already been 'applied' once or if they are misinterpreting the API where color names are attributes that return callable objects.","error":"TypeError: 'ColoredString' object is not callable"},{"fix":"Install the package using pip: `pip install crayons`","cause":"This error indicates that the `crayons` package has not been installed in the Python environment you are currently using, or the environment is not correctly configured.","error":"ModuleNotFoundError: No module named 'crayons'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.4.0","cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/MasterOdin/crayons","docs":null,"changelog":null,"pypi":"https://pypi.org/project/crayons/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["observability","testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-08-29","next_check":"2026-07-28","install_tag":null}}