Sina
Sina

Reputation: 645

How to have colors in Terminal with Python in VSCode?

I'm using VSCode for coding Python. The problem is VSCode prints any output (Errors, Warning and ...) in the same format and color.

Is there any extension or tools to manage it? for example like PyCharm print errors with Red color, warning with yellow and so on?

Upvotes: 24

Views: 20306

Answers (5)

Shane S
Shane S

Reputation: 2303

With the current version of VS Code and a lot of the other emulators there are options to print colored text with out installing special modules.

print("\033[31mThis is red font.\033[0m")
print("\033[32mThis is green font.\033[0m")
print("\033[33mThis is yellow font.\033[0m")
print("\033[34mThis is blue font.\033[0m")
print("\033[37mThis is the default font.\033[0m")

I found some additional information on this topic you can have colored backgrounds. When you choose a background make sure the font is a contrasting color so that it is visible.

print("\033[41m Highlighted Red Text\033[0m")
print("\033[42m Highlighted Green Text\033[0m")
print("\033[43m Highlighted Yellow Text\033[0m")
print("\033[44m Highlighted Blue Text\033[0m")
print("\033[45m Highlighted Magenta Text\033[0m")
print("\033[46m Highlighted Cyan Text\033[0m")
print("\033[47m Highlighted Gray Text\033[0m")

To combine the background and the text color separate it with a ";". For example Grey text on a Highlighted Yellow background...

print("\033[30;43m Grey text on a Highlighted Yellow background \033[0m")

Upvotes: 5

Mildly Intelligent
Mildly Intelligent

Reputation: 83

print("\033[00m00: nothing\033[0m")
print("\033[01m01: bold?\033[0m")
print("\033[02m02: gray\033[0m")
print("\033[03m03: italic\033[0m")
print("\033[04m04: underline\033[0m")
print("\033[05m05-06: nothing\033[0m")
print("\033[07m07: inverted\033[0m")
print("\033[08m08: invisible?\033[0m")
print("\033[09m09: strike through\033[0m")
print("\033[10m10-20: nothing\033[0m")
print("\033[21m21: double underline\033[0m")
print("\033[22m22-29: nothing\033[0m")
print("\033[30m30: gray\033[0m")
print("\033[31m31: red\033[0m")
print("\033[32m32: green\033[0m")
print("\033[33m33: yellow\033[0m")
print("\033[34m34: blue\033[0m")
print("\033[35m35: magenta\033[0m")
print("\033[36m36: cyan\033[0m")
print("\033[37m37+: nothing\033[0m")

Upvotes: 2

Saurabh Jain
Saurabh Jain

Reputation: 1712

Check out this library. This will enable you to use formatted output in the terminal.

Upvotes: 2

barshopen
barshopen

Reputation: 1321

That's not a problem with vscode, but general thing with bash/powershell/cmd (kind reminder that the console that vscode uses, is driven by powershell/bash).

I think I managed to find a fine solution/mitigation for your problem. I found this answer which gave nice results.

enter image description here TBH I don't like this IPython look. Never did. After some research I came up with this improved code

import sys
from IPython.core.ultratb import ColorTB

sys.excepthook = ColorTB()

Which colored well for me on Windows: enter image description here But adding this code each time will be... Very annoying. We should find a way to make it run on any .py on vscode.

I found a way to make any Python file run some line of code before running any script. Go to your python PythonXY\Lib\site-packages where XY is your python version. Add file named exactly sitecustomize.py, and add our improved script.

Test it by printing non-existent variable print(a) and you should see color :)

Upvotes: 9

Mrinal Roy
Mrinal Roy

Reputation: 989

There is no such extension in the VS Code. You have to use Python libraries to do so. For showing different logs in different colors use pip install coloredlogs.

Example from documenation:

import coloredlogs, logging
logger = logging.getLogger(__name__)
coloredlogs.install(level='DEBUG')

logger.debug("this is a debugging message")
logger.info("this is an informational message")
logger.warning("this is a warning message")
logger.error("this is an error message")
logger.critical("this is a critical message")

If you're a Windows user and if the above doesn't work then you've to use an additional dependency pip install colorama.

Upvotes: 0

Related Questions