Skip to content

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

@tomaarsen

Description

@tomaarsen

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions