Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

colorama 4.6 just_fix_windows_console() doesn't exist #370

Open
DanZerom opened this issue Feb 13, 2023 · 5 comments
Open

colorama 4.6 just_fix_windows_console() doesn't exist #370

DanZerom opened this issue Feb 13, 2023 · 5 comments

Comments

@DanZerom
Copy link

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

@njsmith
Copy link
Collaborator

njsmith commented Feb 13, 2023 via email

@DanZerom
Copy link
Author

DanZerom commented Feb 13, 2023 via email

@zackees
Copy link

zackees commented Mar 4, 2023

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
Copy link

zackees commented Mar 6, 2023

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
Copy link

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

#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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants