I have tried colorama
, termcolor
and printy
.
When I run the application in debug method, the text isn't formatted.
This wasn't the case, when I built a C# Console application in VS.
Here is the code using printy
.
from printy import printy
printy("Hello world", "rBU")
Output:
[38;5;196;1;4mHello world[0m
I have tried a few no-package solutions from here, but no luck. How do I print colored text to the terminal?
Is there any VS setting to configure?
I have tried colorama
, termcolor
and printy
.
When I run the application in debug method, the text isn't formatted.
This wasn't the case, when I built a C# Console application in VS.
Here is the code using printy
.
from printy import printy
printy("Hello world", "rBU")
Output:
[38;5;196;1;4mHello world[0m
I have tried a few no-package solutions from here, but no luck. How do I print colored text to the terminal?
Is there any VS setting to configure?
Share Improve this question edited Mar 17 at 7:44 mkrieger1 23.5k7 gold badges64 silver badges82 bronze badges asked Mar 17 at 6:42 sukeshsukesh 2,41112 gold badges64 silver badges130 bronze badges 1- FYI this question should be closed, as it's a duplicate of this one. And no, you should not be copying the answer from that question into your own answer. – David Makogon Commented Mar 28 at 15:25
3 Answers
Reset to default 1It looks as though your terminal doesn't support ANSI escape codes.
Try running this. If it doesn't appear in emboldened red then you can be sure that you're using an inappropriate terminal for your needs:
ESC = "\x1b" # escape
CSI = ESC + "[" # control sequence introducer
RED = CSI + "31m" # foreground colour (red)
BOLD = CSI + "1m"
RESET = CSI + "0m" # reset (all attributes off)
def print_red_bold(s: str) -> None:
print(f"{BOLD}{RED}{s}{RESET}")
print_red_bold("Hello world!")
Your problem is that the code is running in a terminal emulator that does not seem to support VT100 control codes. Try using a different terminal, such as the one built into vscode.
@moduloking's answer here worked for me.
Here are the steps I have implemented:
1. Open the Registry editor. [Press Win + R, type regedit and press Enter]
2. Navigate to this path: HKEY_CURRENT_USER\\Console
3. Right-click the Console key, select New
and DWORD (32-bit) Value
4. Name the DWORD
that you made VirtualTerminalLevel
5. Right-click the name, pressing Modify..., set the Value data to 1
.
6. Save the changes and close the editor.
7. In your code, add this import statement - from colorama import Fore
, just_fix_windows_console
8. Add a method to test, something like this:
def print_formatted_text() -> None:
just_fix_windows_console()
print(Fore.RED, "red text")
9. Run your application and verify if it works!
10. Add the just_fix_windows_console()
in the initializing method of the code,
if the entire program needs formatted text.