Closed nickums closed 1 year ago
I had the same issue, check out this post: https://stackoverflow.com/questions/9848889/colorama-for-python-not-returning-colored-print-lines-on-windows You have to import init, e.g.
from colorama import init,Fore
init(convert=True) print(Fore.RED + 'This is red')
maybe it helps
thanks but that didn't work, and executing just_fix_windows_console() as well (as suggested in the doco) made no difference. Nohing but default color.
Delgan has suggested init(strip=True, wrap=True) Thanks, but as before I only get the same default color
thanks for the suggestion but I still only get the default color.
On Sun, 27 Aug 2023 at 17:54, Delgan @.***> wrote:
@nickums https://github.com/nickums What about init(strip=True, wrap=True)?
— Reply to this email directly, view it on GitHub https://github.com/tartley/colorama/issues/380#issuecomment-1694713338, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADYVMFKJAWVLDH4ZBIT4D3DXXN3S7ANCNFSM6AAAAAA3N3Z3RY . You are receiving this because you were mentioned.Message ID: @.***>
-- Nick "Mac" McElwaine
Delgan has suggested init(strip=True, wrap=True) Thanks, but as before I only get the same default color
Yeah, sorry. I deleted my message shortly after posting it because I realized it was basically the same that what you already tried.
Which terminal are you using (Windows PowerShell, Command prompt, other)?
Try the following:
from colorama import AnsiToWin32
import sys
stream = AnsiToWin32(sys.__stderr__, convert=True, strip=True, autoreset=False).stream
stream.write("\033[91mHello world\033[0m\n")
thanks for trying to help. My printed output is always in the python shell. Maybe that's my problem. Your suggested code gives: stream.write("\033[91mHello world\033[0m\n") File "C:\Python\Python38\lib\site-packages\colorama\ansitowin32.py", line 47, in write self.__convertor.write(text) File "C:\Python\Python38\lib\site-packages\colorama\ansitowin32.py", line 177, in write self.write_and_convert(text) File "C:\Python\Python38\lib\site-packages\colorama\ansitowin32.py", line 202, in write_and_convert self.write_plain_text(text, cursor, start) File "C:\Python\Python38\lib\site-packages\colorama\ansitowin32.py", line 210, in write_plain_text self.wrapped.write(text[start:end]) AttributeError: 'NoneType' object has no attribute 'write'
Weird. Is sys.__stderr__
actually equals to None
on your system?
import sys
print(sys.__stderr__)
print(sys.__stdout__)
Ha! they are both None
I don't know exactly how you're launching Python, but according to the documentation, sys.__stderr__
can be None
when using pythonw.exe
instead of python.exe
:
Note: Under some conditions
stdin
,stdout
andstderr
as well as the original values__stdin__
,__stdout__
and__stderr__
can beNone
. It is usually the case for Windows GUI apps that aren’t connected to a console and Python apps started with pythonw.
It could explain why Colorama isn't able to add colors to your output.
Ok, thanks for the clarification.
On Mon, 28 Aug 2023 at 15:36, Delgan @.***> wrote:
I don't know exactly how you're launching Python, but according to the documentation https://docs.python.org/3/library/sys.html#sys.__stderr__, sys.stderr can be None when using pythonw.exe instead of python.exe:
Note: Under some conditions stdin, stdout and stderr as well as the original values stdin, stdout and stderr can be None. It is usually the case for Windows GUI apps that aren’t connected to a console and Python apps started with pythonw.
It could explain why Colorama isn't able to add colors to your output.
— Reply to this email directly, view it on GitHub https://github.com/tartley/colorama/issues/380#issuecomment-1695815847, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADYVMFKY5PIHJWSNHX72P3DXXSUHNANCNFSM6AAAAAA3N3Z3RY . You are receiving this because you were mentioned.Message ID: @.***>
-- Nick "Mac" McElwaine
When I executed in the windows console instead of the IDLE shell, Your AnsiToWin code worked but my efforts before that did not. I'm drawing a line under this now. Many thanks for your time and effort helping me out.
Your welcome. :+1:
I am using the current colorama 0,4,6 in Python 3.8 on windows10. It works on my grandson's identical environment but not on mine, despite efforts based on the documentation I was forced to use init(strip but still only default color. Also WinStyle does not import. Here is the code:
from colorama import just_fix_windows_console, Fore, Back, Style # python 3.8 colorama 0.4.6 print(Fore.RED + 'red' + Fore.BLUE + 'blue' + Fore.GREEN + 'green') # shows the ASII sequences just_fix_windows_console() print(Fore.RED + 'red' + Fore.BLUE + 'blue' + Fore.GREEN + 'green') # still shows the ASII sequences from colorama import init init(strip=True) # now the ascii seqs are stripped but the output is still defailt print(Fore.RED + Style.BRIGHT + "Hello" + Fore.RESET + Back.RED +" world!" + Back.RESET) from colorama import WinStyle # to do ITALIC cannot import print(Fore.GREEN + WinStyle.ITALIC + "Hello")
Output: [31mred[34mblue[32mgreen [31mred[34mblue[32mgreen Hello world! Traceback (most recent call last): File .... from colorama import WinStyle # to do ITALIC cannot import ImportError: cannot import name 'WinStyle' from 'colorama' (C:\Python\Python38\lib\site-packages\colorama__init__.py)