0

I have the following:

blocky = u'\u2588'
print blocky

When I run it in the command line everything is fine:

# python foo.py
█

Then I run pyinstaller foo.py. No errors. When I run the executable I get this error:

# ./foo
Traceback (most recent call last):
  File "testall.py", line 2, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2588' in position 0: ordinal not in range(128)
[8029] Failed to execute script testall

I've read the python unicode how-to and am still totally confused.

Edit: Just to clarify, I am especially confused about why it works before compiling but not after

MBT
  • 21,733
  • 19
  • 84
  • 102
WarBro
  • 305
  • 2
  • 10
  • 1
    Possible duplicate of [Why can't Python print Unicode symbols?](https://stackoverflow.com/questions/14456908/why-cant-python-print-unicode-symbols) – Daniel Pryden Nov 28 '18 at 20:58
  • Eh, not quite. Especially since I *have* read the how-to. See my solution below. – WarBro Nov 28 '18 at 21:00

1 Answers1

0

OK, so I found the answer. It is apparently known that python gets confused about encodings and likes defaulting to ascii. I believed that adding the initial u was how to explicitly declare the encoding but I guess not? Anyway, modifying it to

blocky = u'\u2588'
print blocky.encode("utf-8")

solved it.

WarBro
  • 305
  • 2
  • 10
  • 1
    No, that doesn't solve it, especially not in a newer version of Python, where you'll just end up trying to print a `bytes` object. – Daniel Pryden Nov 28 '18 at 21:00
  • Well I just tried it and it worked. And since I'm compiling I'm not really worried about the version of python. I am still confused about why it works *before* compiling but not *after* though.... – WarBro Nov 28 '18 at 21:02
  • It probably shouldn't have worked beforehand either, but you got lucky because you are writing bytes and they happen to be valid UTF-8 even though you aren't encoding them. You are almost certainly using Python 2.x and not setting a proper encoding mark in the file. – Daniel Pryden Nov 28 '18 at 21:03
  • Yeah, python is 2.7 and my file is simply those 2 lines. – WarBro Nov 28 '18 at 21:05