0

Working on Ubuntu 16.04, python 2.7.12, I have this code (codec.py):

#!usr/bin/env python
# -*- coding: utf-8 -*-

import codecs

text = u'pi: \u03c0'
print text

Start program with: python codec.py work fine, and display:

pi: π

After compile this code with pyinstaller 3.3 I try to execute program with ./codec, but receive this error:

   Traceback (most recent call last):
      File "codec.py", line 7, in <module>
   UnicodeEncodeError: 'ascii' codec can't encode character u'\u03c0' in position 4: ordinal not in range(128)
   [6816] Failed to execute script codec

I have no solution for that issue. Have you ?

Solution was to modify line 7:

print text.encode("utf-8")

After compile with pyinstaller, program work fine, without error.

popad
  • 13
  • 1
  • 7

1 Answers1

0

Have you tried using IDLE 3 instead of 2.7? I find a lot of the issues stem from using the wrong python versions due to library incompatibility.

Also take a look at : Python Unicode Encode Error

Maybe your syntax is incorrect?

Arvin
  • 11
  • 4
  • Program work fine when is started with python codec.py. Problem is with the compiled program. – popad Nov 06 '17 at 11:19
  • oh okay try setting the encoding to a default value. import sys reload(sys) sys.setdefaultencoding('utf-8') – Arvin Nov 06 '17 at 11:40
  • Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import sys >>> sys.setdefaultencoding('utf-8') Traceback (most recent call last): File "", line 1, in sys.setdefaultencoding('utf-8') AttributeError: 'module' object has no attribute 'setdefaultencoding' >>> – popad Nov 06 '17 at 11:46
  • Solution come from : https://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script , second answer: print text.encode("utf-8") . After this modify I compile again and program run fine now. Thank Harvey, looking for sys.setdefaultencoding, I find this answer. – popad Nov 06 '17 at 11:55