18

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Sina
  • 575
  • 1
  • 5
  • 23

4 Answers4

8

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

barshopen
  • 1,190
  • 2
  • 15
  • 28
  • (+1) this worked well for me. Regarding the `sitecustomize.py` file, on Windows, I placed the code `import sys; from IPython.core.ultratb import ColorTB; sys.excepthook = ColorTB()` inside the file `C:\Users\\miniconda3\envs\\lib\site-packages\sitecustomize.py`. More info about the `sitecustomize.py` file can be found [here](https://docs.python.org/3/library/site.html). – mhdadk Mar 29 '23 at 19:30
2

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

Shayan Shafiq
  • 1,447
  • 5
  • 18
  • 25
Saurabh Jain
  • 1,600
  • 1
  • 20
  • 30
2

With the current version of VS Code there is options to print to color 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. Anything above 37m is default. \033[0m")
Shane S
  • 1,747
  • 14
  • 31
0

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.

Mrinal Roy
  • 969
  • 7
  • 12