tartley / colorama

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

colorama 4.6 just_fix_windows_console() doesn't exist #370

Open DanZerom opened 1 year ago

DanZerom commented 1 year ago

I have tried to use this function both on interpreter and pycharm, it doesn't even appear on method menu.

njsmith commented 1 year ago

What do you get if you run: print(colorama.version)

On Sun, Feb 12, 2023 at 11:58 PM DanZerom @.***> wrote:

I have tried to use this function both on interpreter and pycharm, it doesn't even appear on method menu.

— Reply to this email directly, view it on GitHub https://github.com/tartley/colorama/issues/370, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEU42GQODXWWPVDEEBLYMDWXHSQTANCNFSM6AAAAAAUZ5VDVA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Nathaniel J. Smith -- https://vorpus.org http://vorpus.org

DanZerom commented 1 year ago

Hello,

0.4.6

Regards, Daniel.

W dniu poniedziałek, 13 lutego 2023 Nathaniel J. Smith < @.***> napisał(a):

What do you get if you run: print(colorama.version)

On Sun, Feb 12, 2023 at 11:58 PM DanZerom @.***> wrote:

I have tried to use this function both on interpreter and pycharm, it doesn't even appear on method menu.

— Reply to this email directly, view it on GitHub https://github.com/tartley/colorama/issues/370, or unsubscribe < https://github.com/notifications/unsubscribe-auth/AAEU42GQODXWWPVDEEBLYMDWXHSQTANCNFSM6AAAAAAUZ5VDVA

. You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- Nathaniel J. Smith -- https://vorpus.org http://vorpus.org

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.< https://ci3.googleusercontent.com/proxy/Xi06WAMZDr0019LgqOL1nniBTIi3bF95EGXmIim6RYDX5UpZme5mq2eqEcpdr_oGhNxpcTWC6iKGEDhYgZ-4zkBbuTRY6jgA1d2n3NsCR453t5i0PZd9nBfew2q3pkLGDQ35CBlfjz9OnuYWhve7frMa74lOaByc6i-t0BWtwCeJo3nWjJa0EAhpqsI-Ey-8BfEo7O9Ryi6EQIwXY69i3KX0f__45rbkx-q-Y9omnbPoNdf25Q=s0-d-e1-ft#https://github.com/notifications/beacon/AZHAY36YCTY7WSYUTE4DONTWXHW5PA5CNFSM6AAAAAAUZ5VDVCWGG33NNVSW45C7OR4XAZNMJFZXG5LFINXW23LFNZ2KUY3PNVWWK3TUL5UWJTSVC2IY6.gif>Message ID: @.***>

zackees commented 1 year ago

Yes, also hitting this bug. Pylint said just_fix_windows_console was missing. It works when I launch ubuntu docker but I can't launch it locally in windows.

ImportError: cannot import name 'just_fix_windows_console' from 'colorama'

I have to use init() instead and that works fine. I have the latest. Others in stack overflow mention that downgrading works but this doesn't work for me. I have to use init()

https://stackoverflow.com/questions/74804070/importerror-cannot-import-name-just-fix-windows-console-from-colorama

zackees commented 1 year ago

Just hit this on my Ubuntu 22 LTS box as well. Again, init() works fine so I'm able to work around the issue and update my libs that are hitting this.

muenchenscott commented 1 year ago

For what it's worth: A brief look into the sources here and a couple commands in the interpreter gives you YOUR system's answer to the issue:

  1. Read the CHANGELOG.rst file in the project root.

0.4.6 Current release

https://github.com/tartley/colorama/pull/139 Add alternative to init(), called just_fix_windows_console. This fixes many longstanding problems with init, such as working incorrectly on modern Windows terminals, and wonkiness when init gets called multiple times. The intention is that it just makes all Windows terminals treat ANSI the same way as other terminals do. Many thanks to njsmith for fixing our messes.

  1. The function in question is actually in the initialise.py module of colorama 0.4.6

You can import and run it by using:

>>> from colorama import initialise
>>> initialise.just_fix_windows_console()
  1. This is a partial excerpt from just_fix_windows_console() in the initialise.py module with some extra comments:
global fixed_windows_console

if sys.platform != "win32":                  # Windows 7  and higher should return  TRUE for 'sys.platform == "win32"
    return                                             # so if this is you,  this function will continue to run

if fixed_windows_console:                 # This hasn't been determined yet on first execution and is initially set to FALSE
    return                                             # So nope, you're still going...

# So here we're asking if there is a valid wrapper to write to (ie. is there a valid object to handle encoding/decoding text
if wrapped_stdout is not None or wrapped_stderr is not None:
    # Someone already ran init() and it did stuff, so we won't second-guess them    (Colorama comment)
    return

# On newer versions of Windows, AnsiToWin32.__init__ will implicitly enable the     (Colorama comment)
# native ANSI support in the console as a side-effect. We only need to actually
# replace sys.stdout/stderr if we're in the old-style conversion mode.

new_stdout = AnsiToWin32(sys.stdout, convert=None, strip=None, autoreset=False)
if new_stdout.convert:
    sys.stdout = new_stdout
new_stderr = AnsiToWin32(sys.stderr, convert=None, strip=None, autoreset=False)
if new_stderr.convert:
    sys.stderr = new_stderr

fixed_windows_console = True

I'll let you debug how your system manages the remainder of the code.

  1. If you're running this from from a docker in Ubuntu 04.22 LTS or when running Ubuntu in WSL 2.0), then you are a rare bird indeed, as sys.platform() should be returning 'linux'. See Python Standard Library for additional information about sys.platform.

Cheers scott