Registry / observability / crayons

crayons

JSON →
library0.4.0pypypi✓ verified 27d ago

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.

pip install crayons
INSTALL
IMPORT
SIG · CRAYONS
C
crayons
observabilitypythonv0.4.0
Install
1.7s avg
Import
22ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.4.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.016s · 18MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.028s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

crayons
✓ import crayons
The library is imported as 'crayons', and color functions are accessed as attributes, e.g., `crayons.red()`.

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.

import crayons import os # Basic color usage print(crayons.red("This text is red.")) print(crayons.green("This text is green and bold.", bold=True)) print(crayons.blue("This is blue text.")) # Using 'always=True' to force color even when not in a TTY print(crayons.yellow("This warning should always be yellow.", always=True)) # Demonstrating the clean function colored_text = crayons.magenta("Hello, colorful world!") print(f"Original: {colored_text}") print(f"Cleaned: {crayons.clean(colored_text)}") # Force color using environment variable (useful for CI/CD or non-TTY environments) # os.environ['CLINT_FORCE_COLOR'] = '1' # print(crayons.cyan("This should be cyan even if not in a TTY due to env var.")) # del os.environ['CLINT_FORCE_COLOR'] # Clean up environment variable for subsequent tests
Debug
Known issues
breakingThe `clean` function was completely rewritten in v0.3.0. Previously, it might have stripped all characters, but now it specifically removes ANSI escape codes.
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.
affects: <0.3.0
gotchaAs 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.
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.
affects: >=0.4.0
gotchaPrior to v0.3.1, passing non-string parameters directly to `crayons` functions could lead to unexpected behavior (e.g., issues with `len`).
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.
affects: <0.3.1
Errors
Common errors & fixes
No colors will be displayed if the current process is not in a TTY
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.
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.
AttributeError: module 'crayons' has no attribute 'purple'
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`.
fix
Use one of the supported color attributes, e.g., `crayons.magenta('Hello, World!')`.
TypeError: 'ColoredString' object is not callable
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.
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.
ModuleNotFoundError: No module named 'crayons'
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.
fix
Install the package using pip: `pip install crayons`
Upgrade
Version history
0.4.0latest on PyPI · released Aug 26, 2020
Audit
Dependencies
coloramarequiredProvides cross-platform colored terminal text, powering crayons' functionality.
Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
1
Resources
crayons — pip install crayons · libregistry