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

Error when redirecting output on Windows #16

Closed
DManowitz opened this issue Dec 15, 2021 · 6 comments
Closed

Error when redirecting output on Windows #16

DManowitz opened this issue Dec 15, 2021 · 6 comments

Comments

@DManowitz
Copy link

When I try to redirect the output of "conda-tree deptree" to a file or pipe through more or tee on Windows 10, I get the following error:

Traceback (most recent call last):
File "C:\Users\manow\anaconda3\Scripts\conda-tree-script.py", line 9, in
sys.exit(main())
File "C:\Users\manow\anaconda3\lib\site-packages\conda_tree.py", line 345, in main
print(''.join(complete_tree), end='')
File "C:\Users\manow\anaconda3\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 21-22: character maps to

However, if I just let the output go to the console, I have no problem.

@DManowitz
Copy link
Author

FYI, I tested this both from CMD and from bash (installed from posix conda meta-package), and I get the same error in either.

@rvalieris
Copy link
Collaborator

hello,

I don't use windows, so I'm not sure what this means, does this also happens with other python scripts ?
does setting set PYTHONIOENCODING=utf-8 helps ?

@DManowitz
Copy link
Author

DManowitz commented Dec 16, 2021 via email

@rvalieris
Copy link
Collaborator

currently colorama is only being used to dim the package versions, like a dark gray-ish color, I don't know if cmd supports this.

there are 2 issues here I think:

  1. there is a colorama.init() that detects windows and change the escape codes, the script is not calling this currently, so the escape codes are probably wrong.
  2. the deptree output uses box drawing characters, without knowing the encoding, these characters might show up as gibberish in the file

try this:

  • add import colorama; colorama.init() to the top of the script
  • try setting PYTHONIOENCODING=utf-8 or PYTHONIOENCODING=utf-16 and check the file results

@DManowitz
Copy link
Author

DManowitz commented Dec 29, 2021 via email

rvalieris added a commit that referenced this issue Jan 17, 2022
to avoid issues on windows
@prusswan
Copy link

prusswan commented Nov 21, 2024

More details can be found at tartley/colorama#319

cp1252.py is not showing the exact characters, but once I added import logging to that file, the error is thrown in mbcs.py instead:

UnicodeEncodeError: 'mbcs' codec can't encode character '\u251c' in position 14: invalid character

Instead of setting PYTHONIOENCODING=utf-8, an alternative (from here) is to add

sys.stdout.reconfigure(encoding='utf-8')

to the top of conda_tree.py

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

3 participants