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

isort 5.11.0, NameError: name 'colorama' is not defined #2031

Closed
tomaarsen opened this issue Dec 12, 2022 · 5 comments · Fixed by #2032
Closed

isort 5.11.0, NameError: name 'colorama' is not defined #2031

tomaarsen opened this issue Dec 12, 2022 · 5 comments · Fixed by #2032

Comments

@tomaarsen
Copy link
Contributor

tomaarsen commented Dec 12, 2022

Hello!

Bug overview

  • colorama.init is always called in create_terminal_printer, even if colorama_unavailable is set to True and colorama is unavailable.

Details

A Printer is initialized in this function:

isort/isort/format.py

Lines 138 to 156 in 6525008

def create_terminal_printer(
color: bool, output: Optional[TextIO] = None, error: str = "", success: str = ""
) -> BasicPrinter:
if color and colorama_unavailable:
no_colorama_message = (
"\n"
"Sorry, but to use --color (color_output) the colorama python package is required.\n\n"
"Reference: https://pypi.org/project/colorama/\n\n"
"You can either install it separately on your system or as the colors extra "
"for isort. Ex: \n\n"
"$ pip install isort[colors]\n"
)
print(no_colorama_message, file=sys.stderr)
sys.exit(1)
colorama.init(strip=False)
return (
ColoramaPrinter(error, success, output) if color else BasicPrinter(error, success, output)
)

As you can see, colorama_unavailable is a global variable in this context. It should be used to ensure that colorama is not used if set to True. However, this is not done.

How to reproduce

Ensure the following:

Then, any use of isort --check-only demo.py, with demo.py as an existing file, should trigger the following error:

Traceback (most recent call last):
  File "[sic]\site-packages\isort\main.py", line 87, in sort_imports
    incorrectly_sorted = not api.check_file(file_name, config=config, **kwargs)
  File "[sic]\site-packages\isort\api.py", line 338, in check_file
    return check_stream(
  File "[sic]\site-packages\isort\api.py", line 272, in check_stream
    printer = create_terminal_printer(
  File "[sic]\site-packages\isort\format.py", line 153, in create_terminal_printer
    colorama.init(strip=False)
NameError: name 'colorama' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "[sic]\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "[sic]\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "[sic]\site-packages\isort\__main__.py", line 3, in <module>
    main()
  File "[sic]\site-packages\isort\main.py", line 1225, in main
    for sort_attempt in attempt_iterator:
  File "[sic]\site-packages\isort\main.py", line 1209, in <genexpr>
    sort_imports(  # type: ignore
  File "[sic]\site-packages\isort\main.py", line 114, in sort_imports
    _print_hard_fail(config, offending_file=file_name)
  File "[sic]\site-packages\isort\main.py", line 127, in _print_hard_fail
    printer = create_terminal_printer(
  File "[sic]\site-packages\isort\format.py", line 153, in create_terminal_printer
    colorama.init(strip=False)
NameError: name 'colorama' is not defined

In the wild

See this Github Actions for the error in the wild.

  • Tom Aarsen
@tomaarsen
Copy link
Contributor Author

A workaround right now is to just install colorama:

pip install colorama

Alternatively, you may need to add it as an additional download or dependency in your build pipeline, if (like me) you're experiencing this bug in your CI pipeline.

@jgould22
Copy link

A workaround right now is to just install colorama:

pip install colorama

Alternatively, you may need to add it as an additional download or dependency in your build pipeline, if (like me) you're experiencing this bug in your CI pipeline.

pip install isort[colors] , as the message above suggests also worked for me

@staticdev
Copy link
Collaborator

5.11.1 is here.

fmigneault added a commit to Ouranosinc/Magpie that referenced this issue Dec 12, 2022
fmigneault added a commit to Ouranosinc/Magpie that referenced this issue Dec 12, 2022
@tomaarsen
Copy link
Contributor Author

Can confirm that 5.11.1 solves the issue for me. Thanks for the quick response @staticdev!

@hauntsaninja
Copy link

Thanks for the quick fix!

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

Successfully merging a pull request may close this issue.

4 participants