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

Colors do not work if tqdm imported. #678

Closed
krishnachouhan opened this issue Feb 19, 2019 · 13 comments
Closed

Colors do not work if tqdm imported. #678

krishnachouhan opened this issue Feb 19, 2019 · 13 comments
Assignees
Labels
invalid ⛔ Not-an-issue or upstream (not-our-issue) p2-bug-warning ⚠ Visual output bad to-fix ⌛ In progress

Comments

@krishnachouhan
Copy link

This code works...

_CRED = '\033[91m'
_CEND = '\033[0m'
print(_CRED + "Colors bre being set" + _CEND + '-all_set')

but after importing the same code does not work.

import tqdm
_CRED = '\033[91m'
_CEND = '\033[0m'

print(_CRED + "Colors bre being set" + _CEND + '-all_set')

Python:
(base) C:\Users\212574384>python --version
Python 3.7.0
OS: win 10

image

@casperdcl
Copy link
Sponsor Member

just to confirm, if you import colorama instead of import tqdm, does it still break?

@krishnachouhan
Copy link
Author

krishnachouhan commented Feb 20, 2019

colorama works fine.

image

!sorry about closing, very new to this..

@svlandeg
Copy link

svlandeg commented Sep 9, 2019

We ran into this issue as well because spaCy uses tqdm (explosion/spaCy#4200). We found that this bug seems to happen specifically on Windows, and not on other OS's.

@casperdcl casperdcl added help wanted 🙏 We need you (discussion or implementation) question/docs ‽ Documentation clarification candidate submodule-notebook 📓 Much web such IDE labels Sep 10, 2019
@casperdcl
Copy link
Sponsor Member

Found the issue - for now, please use:

import tqdm  # or whatever tqdm imports you use
import colorama
colorama.deinit()
colorama.init(strip=False)

Will patch soon...

@casperdcl casperdcl self-assigned this Nov 1, 2019
@casperdcl casperdcl added p2-bug-warning ⚠ Visual output bad invalid ⛔ Not-an-issue or upstream (not-our-issue) to-fix ⌛ In progress and removed help wanted 🙏 We need you (discussion or implementation) question/docs ‽ Documentation clarification candidate submodule-notebook 📓 Much web such IDE labels Nov 1, 2019
@casperdcl
Copy link
Sponsor Member

tartley/colorama#79

@H4CKY54CK
Copy link

H4CKY54CK commented Oct 10, 2020

I've been experiencing this issue for a long time. I just happened across this page, but the proposed temporary fix is not working for me. Has anything changed since then? I'm on Python 3.8, I just updated tqdm and colorama via pip, and still experiencing the issue.

Actually, using this on it's own disables my colors entirely.

import colorama
colorama.deinit()
colorama.init(strip=False) # This line seems to be doing the actual disabling.

EDIT: I don't know how relevant this is, but I thought I'd mention that I have VirtualTerminalLevel set to 1 in my registry.

EDIT 2: Sorry for the multiple edits. I'm not familiar with how exactly tqdm uses colorama, but if I omit that last line above, then I get my progress bars, and my colored text. But I don't know if there's something that tqdm now doesn't have that it needs. I don't know, that's why I'm here.

@casperdcl
Copy link
Sponsor Member

I don't know if there's something that tqdm now doesn't have that it needs

It has everything it needs, no worries.

to be clear, are you saying:

  1. colours were working
  2. import tqdm stops colours working
  3. import colorama; colorama.deinit() gets colours working again

@H4CKY54CK
Copy link

H4CKY54CK commented Oct 10, 2020

All of the above.

If I do a simple

print("\x1b[38;2;0;255;0mHello\x1b[0m")

I get a green "Hello".

Once I import tqdm, I lose the color (it still only says "Hello"; it's not doing anything crazy like printing out the ANSI sequence or anything).

If I do

from tqdm import tqdm
import colorama
colorama.deinit()
print("\x1b[38;2;0;255;0mHello\x1b[0m")

I get green text again. But I lose it again with the following.

from tqdm import tqdm
import colorama
colorama.deinit()
colorama.init(strip=False)
print("\x1b[38;2;0;255;0mHello\x1b[0m")

Sorry if that seemed condescending. I just wanted to be explicit.

If this is all normal, then I apologize for commenting on this issue. I can work with this now that I know how to get around the issue, but I suppose you can never have too much data on possible issues.

EDIT: I just thought it was odd that the suggested temporary fix (below) didn't have the expected effect. Unless I misread

Found the issue - for now, please use:

import tqdm  # or whatever tqdm imports you use
import colorama
colorama.deinit()
colorama.init(strip=False)

Will patch soon...

EDIT 2 (lol): Sorry, I'm not complaining, just inquiring.

@casperdcl
Copy link
Sponsor Member

casperdcl commented Oct 10, 2020

hmm this is odd. Looks related to tartley/colorama#79

importing tqdm will internally do colorama.init(strip=False) on Windows.

  1. What's your OS and terminal? I assume OS is Windows, but is the terminal git-bash? Plain command prompt? Notebook?
  2. What does your output look like if you run this in one go?
print("\x1b[38;2;0;255;0mCorrectly green\x1b[0m")
import colorama
print("\x1b[38;2;0;255;0mCorrectly green\x1b[0m")
colorama.init(strip=False)
print("\x1b[38;2;0;255;0mShould be green but isn't\x1b[0m")
colorama.deinit()
print("\x1b[38;2;0;255;0mCorrectly green\x1b[0m")
colorama.init()
print("\x1b[38;2;0;255;0mShould be green but isn't\x1b[0m")

I get this on linux (everything correctly green in terminal as well as notebook):

image

Based on your report I'd take a wild guess that you're using windows and a terminal which can understand ANSI codes but can't understand the windows API calls that colorama produces. Basically tartley/colorama#224 which may be fixed if they merge tartley/colorama#226

@casperdcl casperdcl reopened this Oct 10, 2020
@casperdcl casperdcl added help wanted 🙏 We need you (discussion or implementation) need-feedback 📢 We need your response (question) labels Oct 10, 2020
@H4CKY54CK
Copy link

Windows 10, Python 3.8.5, good old fashioned cmd.

image

@H4CKY54CK
Copy link

H4CKY54CK commented Oct 10, 2020

I don't know if this is relevant, but it all works correctly if I use cmd to ssh into my Raspberry Pi, and do all the same things from there.

I don't know if this is relevent either, but both this laptop and my previous PC, by default, would just print the actual \x1b[38;2;0;255;0mCorrectly green\x1b[0m, rather than actually interpret the sequence. In both cases, I've had to go into my registry and add/change VirtualTerminalLevel to 1. I don't know how common this is, but I've had to do this ever since I got into python over a year ago.

If there's anything else I can do or provide that would be helpful, don't hesitate to ask. I don't mind.

@H4CKY54CK
Copy link

H4CKY54CK commented Oct 10, 2020

It seems to only be eating the "True Color" sequences (forgive me if that's not the correct term) aka \x1b[38;2;r;g;bm.

But surprisingly, I can use 3/4 bit sequences (again, forgive me if not the correct term) just fine aka \x1b[32m and so on.

I'm not sure what this means, if anything, but I thought you should know.

image

EDIT: I just realized, I should probably take this on over to the colorama issues, then? Since this is really no longer a tqdm issue anymore.

Final edit: tartley/colorama#217 My legwork was for naught.

Sorry for bothering you about this. Although, I did finally find something concrete on why this happens.

@casperdcl
Copy link
Sponsor Member

My legwork was for naught.

correctly diagnosing and liking an appropriate issue is not for naught!

Thanks; closing for now as it seems to be an issue with an upstream library.

For now, this may be required on some systems:

import tqdm  # or whatever tqdm imports you use
try:
    import colorama
except ImportError:
    pass
else:
    colorama.deinit()  # breaks some windows systems but fixes other windows systems
    # see https://github.com/tartley/colorama/issues/217

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid ⛔ Not-an-issue or upstream (not-our-issue) p2-bug-warning ⚠ Visual output bad to-fix ⌛ In progress
Projects
None yet
Development

No branches or pull requests

4 participants