{"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":"en","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,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}