pythonwindowsvisual-studio-2022coloramatermcolor

How to print colored or bold text in Python Visual Studio 2022 CLI app


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

enter image description here

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?


Solution

  • @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.