Colorama
Colorama is a Python library that simplifies printing colored text and styles to the terminal, ensuring cross-platform compatibility, especially on Windows. The current version is 0.4.6, released on October 25, 2022. The library has a stable release cadence, with updates addressing compatibility and minor feature enhancements.
Common errors
-
ModuleNotFoundError: No module named 'colorama'
cause The colorama library is not installed in the Python environment you are currently using, or it was installed for a different Python version than the one executing your script.fixInstall the colorama package using pip: `pip install colorama` or `pip3 install colorama`. If using an IDE like VS Code, ensure the correct Python interpreter is selected that has colorama installed. -
AttributeError: module 'colorama' has no attribute 'init'
cause This error often occurs when `init` (or `Fore`, `Back`, `Style`) is not correctly imported from the `colorama` module, or there's a shadowing file named `colorama.py` in your project directory.fixEnsure you are importing `init` directly, for example: `from colorama import init, Fore, Back, Style`. Alternatively, if you `import colorama`, you must call `colorama.init()`. Also, check that you don't have a local file named `colorama.py` conflicting with the installed package. -
Colorama not working / no colors in Windows terminal / PowerShell / VS Code
cause On Windows, Colorama typically needs to initialize itself to convert ANSI escape codes into native Windows console API calls. If `init()` is not called, or if the terminal environment (like some older PowerShell versions or certain IDE integrated terminals) doesn't fully support ANSI escape sequences by default, colors may not display.fixCall `colorama.init()` at the beginning of your script. For persistent issues on Windows, especially older versions or specific terminals, explicitly pass `convert=True` to `init()`: `init(convert=True)`. As of Colorama 0.4.6, `just_fix_windows_console()` can also be used for minimal setup. -
ImportError: cannot import name 'Fore' from 'colorama'
cause This typically happens due to a case sensitivity error, where 'fore' is used instead of 'Fore' (or 'back' instead of 'Back', 'style' instead of 'Style'). Python imports are case-sensitive.fixCorrect the import statement to use the proper capitalization: `from colorama import Fore, Back, Style`.
Warnings
- gotcha On Windows, failing to call 'init()' may result in ANSI escape sequences being displayed as raw text instead of colored output.
- gotcha Using 'init(autoreset=True)' resets styles after each print statement, which may not be desired in all cases.
- gotcha Colorama's 'Style.DIM' may not be supported on all terminals, leading to inconsistent text appearance.
- gotcha Combining Colorama with other ANSI-based libraries without proper initialization may lead to unexpected behavior.
Install
-
pip install colorama
Imports
- init
from colorama import init
- Fore
from colorama import Fore
- Back
from colorama import Back
- Style
from colorama import Style
Quickstart
from colorama import init, Fore, Back, Style
init(autoreset=True)
print(Fore.RED + 'This is red text')
print(Back.GREEN + 'This has a green background')
print(Style.BRIGHT + 'This is bright text')
print('Back to normal text')