Search code examples
pythonchess

Difference in the output of Spyder and Repl.it vs. Visual Studio and Command Line - [34mR vs letter R (with blue collor)


I am using a command-line chess game that I am taking from here. If I run the main.py part of the code using the "repl.it" or anaconda I will get what I expect: enter image description here

But if I try to run via anaconda command line or visual studio I get this: enter image description here


Solution

  • IPython, like many terminal emulators on Unix-Like systems, has built in support for ANSI Color codes. The escape characters you see in the Windows terminal are automatically converted to color commands by IPython.

    You can get similar support for your python programs on Windows using the colorama library. For the output you are attempting, do

    import colorama
    colorama.init()
    

    This will replace sys.stdout and sys.stderr with file objects that strip away escape sequences and perform corresponding Windows CMD operations.

    If you want this to happen outside of Python as well, consider installing ansi.sys on your machine, as the colorama documentation suggests.