Why doesn't Colorama work with Visual Studio?

1.1k Views Asked by At

I'm using the colorama module in Python for Visual Studio and when I run the code it doesn't give errors or anything, but adds ←[36m before each line or 34m or 32m (I'm guessing it's the code for the color) but doesn't color the text. It's still white. I also tried the other form with

    print("\033[1;32;40m Bright Green  \n") ##and
    print('\033[31m' + 'some red text')

Yet, nothing happens it just shows the bracket and number. Can someone explain? Does it not work in Visual Studio?

1

There are 1 best solutions below

0
moduloking On

That is because your VS Code terminal does not have ANSI colors enabled.

There are some ways you can try fixing it:

1. init() Way

If you are on a recent Windows 10 or a better version and your stdout or your stderr are pointing at the Windows console, then try init() which in simple words wraps stdout and stderr. For older OSes, it may be better to check out :

from colorama import Fore, init 
init()
print("\033[1;32;40m Bright Green  \n") ##and
print('\033[31m' + 'some red text')

One good thing about init() is that you can put customizable arguments, if init() did not work, try this one too:

from colorama import Fore, init 
init(convert=True)
print("\033[1;32;40m Bright Green  \n") ##and
print('\033[31m' + 'some red text')

2. just_fix_windows_console() Way

If that did not work or maybe you are looking for another solution, try using just_fix_windows_console(). That is a better solution that also works for older OSes and is better.

If you’re on a recent version of Windows 10 or better, and your stdout/stderr are pointing to a Windows console, then this will flip the magic configuration switch to enable Windows’ built-in ANSI support.

If you’re on an older version of Windows, and your stdout/stderr are pointing to a Windows console, then this will wrap sys.stdout and/or sys.stderr in a magic file object that intercepts ANSI escape sequences and issues the appropriate Win32 calls to emulate them.

Also, this is better because calling init multiple times is unsafe.

Please read more about it in the documentation: here

Here is the code with just_fix_windows_console():

from colorama import Fore, just_fix_windows_console
just_fix_windows_console()
print("\033[1;32;40m Bright Green  \n") ##and
print('\033[31m' + 'some red text')

If none of those work, proceed to the other way:

3. Enable ANSI Colors on VS

VS Code has the ANSI colors enabled in default, try to reinstall it and see. It may be disabled. This may not be the fault of VS. My VS works fine:

PS C:\Users\User> & C:/Users/User/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/User/from colorama import Fore.py"
 Bright Green  

some red text

If it still didn't not work, use another module like termcolor or install a extension that supports ANSI colors.

4. More Advanced Configuration

If nothing worked, if init(), just_fix_windows_console(), using another module or anything mentioned work.

Look at my more complex answer on another post which is here.

5. Last Resort

If nothing provided on this post and the other post worked, try using CMD or PowerShell for this since the other post is special and designed for those terminals. This seems like faulty configuration.