question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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: https://github.com/PyCQA/isort/blob/65250088ca11ea59eba80bfb8e65891e92333edb/isort/format.py#L138-L156

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:

  • Download isort at 5.11.0 (or at any point since #1961 / c1a4139241b757a5af3d85143bbd05cd11721a17)
  • colorama must not be downloaded.

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

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Reactions:14
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jgould22commented, Dec 12, 2022

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

0reactions
hauntsaninjacommented, Dec 12, 2022

Thanks for the quick fix!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix Rich compatibility #1961 - PyCQA/isort - GitHub
isort 5.11.0, NameError: name 'colorama' is not defined #2031. Closed. Sign up for free to join this conversation on GitHub. Already have an...
Read more >
Problems using Colorama on Python 2.7 - Stack Overflow
I'm learning to use colorama in Python, so I installed it and I'm able to import the module with no problems from the...
Read more >
Package List — Spack 0.20.0.dev0 documentation
ACPID reads a set of configuration files which define event->action pairs. ... of the Python standard argparse library (though not necessarily the API)....
Read more >
Debian -- Software Packages in "sid", Subsection python
Software Packages in "sid", Subsection python ... libbasix0.5 (0.5.1-1): Finite Element Basis Function Definition Runtime Library (Python 3) ...
Read more >
Installed Python Libraries - CoCalc
Library Python 3 SageMath Anaconda 2020 Anaco... abelfunctions 0.1.0 access. Calculate spatial accessibility metrics. 1.1.8 1.1.8 1.1.3 1.1.3 admcycles. Tautological ring on Mbar_g,n 1.3.2
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found