Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Use Colors in Terminal (Python) : 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?

like image 932
Sina Avatar asked Sep 20 '19 14:09

Sina


People also ask

How do I add color to VSCode terminal?

VSCode comes with in-built color themes which can be used to change the colors of the editor and the terminal. For changing the color theme press Ctrl + K + T in windows/ubuntu or CMD + K + T on mac.


2 Answers

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 :)

like image 123
barshopen Avatar answered Sep 29 '22 10:09

barshopen


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

like image 41
Saurabh Jain Avatar answered Sep 29 '22 11:09

Saurabh Jain