Color in the command line makes debugging and reading output much easier. But if you’ve ever tried using ANSI escape codes in Python scripts on Windows 10’s CMD.exe, you might have noticed that they don’t work as expected. Instead of colorful output, you get raw escape sequences printed as text.
This guide walks you through enabling ANSI escape codes in Windows 10 so you can bring color to your Python CLI applications.
Why ANSI Escape Codes Don’t Work by Default in Windows CMD.exe
Linux and macOS terminals support ANSI escape codes out of the box, but Windows CMD.exe historically didn’t. Earlier versions of Windows required third-party solutions like ANSICON to emulate ANSI sequences.
Starting from Windows 10 (build 10586), Microsoft introduced proper support for ANSI escape codes. However, they aren’t always enabled by default, leading to issues where expected color output doesn’t appear.
What Are ANSI Escape Codes?
ANSI escape codes are special sequences that change text color, background color, and formatting in terminal output. They begin with the escape character (\x1b
) followed by a series of commands inside square brackets.
For example, this command prints red text:
print("\x1b[31mThis text is red\x1b[0m")
Here’s how ANSI escape sequences work:
\x1b[
: Escape sequence introducer (CSI).31m
: Sets the text color to red.0m
: Resets formatting to default.
Common ANSI Escape Codes
Code | Description |
---|---|
\x1b[1m | Bold |
\x1b[4m | Underline |
\x1b[31m | Red text |
\x1b[44m | Blue background |
\x1b[0m | Reset formatting |
Color Codes
- Foreground Colors: 30–37 (Standard), 90–97 (Bright)
- Background Colors: 40–47 (Standard), 100–107 (Bright)
- 256-Color Mode:
\x1b[38;5;<color_code>m
- True Color (RGB):
\x1b[38;2;<r>;<g>;m
However, none of these will work in CMD.exe without enabling virtual terminal processing.
How to Enable ANSI Escape Codes in CMD.exe
There are multiple ways to enable ANSI color support on Windows.
Method 1: Using os.system('color')
This command resets the console color settings and sometimes enables ANSI support.
import os
os.system('color')
print("\x1b[31mThis text is red\x1b[0m")
🔹 Downside: This doesn’t always work in all CMD environments.
Method 2: Using colorama
(Recommended)
The colorama
library automatically enables ANSI escape code processing on Windows.
Install it first:
pip install colorama
Then use it in your Python script:
from colorama import init, Fore, Back, Style
init()
print(Fore.RED + "This text is red.")
print(Back.GREEN + "This background is green.")
print(Style.BRIGHT + "Bright text.")
print(Style.RESET_ALL + "Back to normal.")
🔹 Why use colorama
?
- No need to manually enable ANSI support.
- Works consistently across different Windows versions.
- Compatible with Linux and macOS too.
Method 3: Enabling ANSI with Windows API (ctypes
)
You can directly enable ANSI processing in CMD.exe using the Windows API.
import ctypes
kernel32 = ctypes.windll.kernel32
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
print("\x1b[1;31mTest\x1b[0m")
🔹 How this works:
- Gets the console handle using
GetStdHandle(-11)
. - Enables
ENABLE_VIRTUAL_TERMINAL_PROCESSING
mode.
This approach is useful if you don’t want external dependencies like colorama
.
Method 4: Conditional Enabling Based on Environment
If your Python script is used in different environments, ensure colors only apply when running interactively.
import sys, os
if sys.stdout.isatty():
os.system('color')
print("\x1b[32mThis is green text\x1b[0m")
Fixing Common Issues
If ANSI escape codes still don’t work, try these troubleshooting steps:
- Ensure CMD.exe is updated (older versions lack ANSI support).
- Try running
python
insidecmd.exe
instead of PowerShell (PowerShell may handle ANSI differently). - Run scripts with
start /B python script.py
if needed. - Check terminal type using
echo %TERM%
(should be compatible with ANSI). - Try a different terminal emulator, like Windows Terminal or ConEmu.
Taking It Further with Fancy Output
If you want better-structured and visually appealing output, consider using the rich
library:
pip install rich
from rich.console import Console
console = Console()
console.print("[bold red]Error:[/] Something went wrong!")
This provides rich text formatting, boxes, and progress bars.
Now Your CMD.exe Can Display Colors
Windows 10 now supports ANSI codes, but they need manual enabling.
- The easiest method is to use colorama.
- If needed, ctypes can enable ANSI directly.
- If colors still don’t show, try checking your terminal settings.
Colorful output makes debugging easier and your CLI applications more readable. Try it out now!
0 Comments