My Issue
I'm a begginer with Python, and I've been working on code in Visual Studio in which I want to be able use ANSI Escape Sequences to format my output on the terminal console. I'm using Python 3.9, Visual Studio 2019 (Community Edition) and Windows 10 (64-bit).
I originally wanted to be able to move the cursor on the command prompt, like how you are able to do in C++ with the SetConsoleCursorPosition
function. I also wanted to make my text colored, if possible. People over the internet suggested the use of the colarama
module, and the Escape Sequences.
I have, however, realized that none of the Escape Sequences appear to work at all in my VS terminal...
My Attempts
Here's an example of a suggestion (from Colorama for Python, Not returning colored print lines on Windows), using the colorama
module, to color my text:
from colorama import init, Fore, Back, Style
init(convert=True)
print(Fore.RED + 'some red text')
Code above just prints in white on my VS terminal. I've also verified that I have colorama
installed properly.
From the same posting, I've also tried:
from colorama import Fore, Style, init
init()
from sys import stdout
stdout.write(Fore.RED + Style.BRIGHT + "Test" + Style.RESET_ALL + "\n")
But it also prints in white.
I've also seen the following suggestion, using the os
module (from ANSI escape code wont work on python interpreter):
import os
os.system("echo [31mThis is red[0m")
Code above prints the following:
[31mThis is red[0m
As far as I understand, in order to enable the Escape Characters in Python, the call os.system("")
needs to be performed.
Attempting the following (from Python: How can I make the ANSI escape codes to work also in Windows?):
import os
os.system("")
COLOR = {
"HEADER": "\033[95m",
"BLUE": "\033[94m",
"GREEN": "\033[92m",
"RED": "\033[91m",
"ENDC": "\033[0m",
}
print(COLOR["GREEN"], "Testing Green!!", COLOR["ENDC"])
Yields the following:
←[92m Testing Green!! ←[0m
As for the moving of the terminal cursor, I've also tried the following (from https://stackoverflow.com/questions/11474391/is-there-go-up-line-character-opposite-of-n#:~:text=%22%5C033%5BF%22%20%E2%80%93,move%20cursor%20up%20one%20line):
import os
os.system("")
print("Previous line")
print("\033[FMy text overwriting the previous line.")
Yielding the following:
Previous line
←[FMy text overwriting the previous line.
As well as trying the following:
import os
os.system("")
print("Previous line")
sys.stdout.write("\033[FMy text overwriting the previous line.")
Giving the same result.
I'm a bit lost at this point, I'm not quite sure why the Escape Characters are not working. I must be doing something very wrong here...
Any guidance is appreciated!