简体   繁体   English

Linux终端上的Python颜色输出不起作用

[英]Python color output on terminal in linux is not working

I have linux server where I am running python script on server console my script colors are not displaying. 我的Linux服务器在服务器控制台上运行python脚本,但脚本颜色未显示。 Same script is working on Putty sessions. 同一脚本正在腻子会话上工作。

I have checked echo $TERM in putty it is "xterm" and server console it is "linux". 我已经检查回声$ TERM在腻子中是“ xterm”,在服务器控制台中是“ linux”。

OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
import os
import subprocess
from subprocess import check_output
from subprocess import call
import sys
import time
from shutil import copyfile
import commands
print OKGREEN + "Testing Green" + ENDC

I have also tried below setting up "os.system('export TERM="xterm"')" in python script but not working 我还在下面尝试在python脚本中设置“ os.system('export TERM =“ xterm”')“,但无法正常工作

Could some one help me to fix this issue. 有人可以帮我解决这个问题。

Version: 2.7 版本:2.7

Note: In same server console vim is displaying with colors 注意:在同一服务器控制台中,vim显示颜色

It works fine for me with this code. 此代码对我来说很好用。

jay@Jay-Linux:~$ /usr/bin/python -V Python 2.7.15rc1 jay @ Jay-Linux:〜$ / usr / bin / python -V Python 2.7.15rc1

#!/usr/bin/env python
#Jay
class bcolors:
        HEADER = '\033[95m'
        OKBLUE = '\033[94m'
        OKGREEN = '\033[92m'
        WARNING = '\033[93m'
        FAIL = '\033[91m'
        ENDC = '\033[0m'
        BOLD = '\033[1m'
        UNDERLINE = '\033[4m'
print bcolors.OKBLUE + 'OK', bcolors.ENDC
print bcolors.HEADER + 'This is header', bcolors.ENDC
print bcolors.FAIL + 'Failed.', bcolors.ENDC
print bcolors.OKGREEN + 'OK Green', bcolors.ENDC
print bcolors.WARNING + 'Warning', bcolors.ENDC
print bcolors.FAIL + 'Failed', bcolors.ENDC

输出量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM