If I open a Command window tab in the Windows Terminal, either of the following methods would work correctly:
print("\U0001F600")
print("\N{grinning face}")
from emoji import emojize
print(emojize(":grinning_face:"))
from rich import print as rich_print
rich_print(":grinning_face:")
However, if I open a command window via Win+R CMD.EXE, neither of these methods works - a question mark in a square is displayed every time. Changing the code page to 65001 does not help, either. Importing just_fix_windows_console from colorama and running it before the printing also doesn't help - although it fixes the ability of printing ANSI escape sequences.
Both the CMD.EXE window and the Command window in Terminal use the same font - Consolas.
Is displaying emojis in a CMD.EXE window simply not possible or am I missing some trick that I don't know?
The issue is not with
cmd.exe, which just outputs byte strings to the terminal it is running in using whatever encoding is being used.So, if you run Python in Command Prompt, and get it to
print("\U0001F600"), that character will be sent to the terminal.However, the default Windows Console Host that used to be what you always opened when you ran Command Prompt on previous versions of Windows, does not support UTF-8 characters by default, it uses another encoding. So, you'll end up seeing this:
However, on Windows 11 the new Windows Terminal is the default, and it does have full support and uses UTF-8 by default. If you run Command Prompt using the Windows terminal, you get this:
It's easy to confuse the command interpreter (the actual CLI application) and the terminal it is running in to show what is going on with the input and output streams for the command interpreter.
PowerShell and Command Prompt are command interpreters. Windows Console Host and Windows Terminal are terminals. It's the choice of the latter that limits what can be rendered. The former both support many encodings.
Note that you can also download and install Windows Terminal on Windows 10 - it's just not the default.
Note: you asked if there's anything you can do. You can and this question goes into that, but also note that this is not recommended and has far-reaching impact on other applications. You definitely wouldn't want to force other users of your Python script to have to do the same. Windows Terminal is here to stay, so using it as the new default is the better option.