tartley / colorama

Simple cross-platform colored terminal text in Python
BSD 3-Clause "New" or "Revised" License
3.51k stars 249 forks source link

colorama not coloring on windows despite efforts #380

Closed nickums closed 1 year ago

nickums commented 1 year ago

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

maupwastaken commented 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

nickums commented 1 year ago

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.

nickums commented 1 year ago

Delgan has suggested init(strip=True, wrap=True) Thanks, but as before I only get the same default color

nickums commented 1 year ago

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 commented 1 year ago

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")
nickums commented 1 year ago

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'

Delgan commented 1 year ago

Weird. Is sys.__stderr__ actually equals to None on your system?

import sys
print(sys.__stderr__)
print(sys.__stdout__)
nickums commented 1 year ago

Ha! they are both None

Delgan commented 1 year ago

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 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.

nickums commented 1 year ago

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

nickums commented 1 year ago

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.

Delgan commented 1 year ago

Your welcome. :+1: