如何在终端(Python)中使用颜色:vscode

时间:2019-09-20 14:36:37

标签: python visual-studio-code pycharm vscode-settings vscode-extensions

我正在使用VSCode编写Python。问题是VSCode以相同的格式和颜色打印任何输出(错误,警告和...)。

是否有任何扩展程序或工具来对其进行管理?例如像Pycharm的红色打印错误,黄色的警告等等?

谢谢。

3 个答案:

答案 0 :(得分:1)

vscode并不是问题,而是bash / powershell / cmd的一般问题(提醒您,vscode使用的控制台由powershell / bash驱动)。

我认为我设法为您的问题找到了很好的解决方案/缓解措施。我发现这个answer给出了不错的结果。

enter image description here TBH我不喜欢这种IPython外观。从没干过。经过一番研究,我想出了这个改进的代码

import sys
from IPython.core.ultratb import ColorTB

sys.excepthook = ColorTB()

在Windows上对我来说颜色很好: enter image description here 但是每次添加此代码将非常烦人。我们应该找到一种方法使其在vscode的任何.py上运行。

我找到了制作任何Python文件run some line of code before running any script的方法。 转到您的Python PythonXY\Lib\site-packages,其中XY是您的python版本。添加名为恰好 sitecustomize.py的文件,并添加改进的脚本。

通过打印不存在的变量print(a)进行测试,您应该看到颜色:)

答案 1 :(得分:1)

查看此library。这将使您能够在终端中使用格式化的输出。

答案 2 :(得分:0)

VS代码中没有此类扩展名。您必须使用Python库来这样做。要使用不同的颜色显示不同的日志,请使用<enum>

documenation中的示例:

pip install coloredlogs

如果您是Windows用户,并且上述方法不起作用,则必须使用additional dependency 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")