jeudi 29 octobre 2015

Compiling with cx freeze and UnicodeDecodeError

I'm new here and I have a little question concerning an error I get while trying to freeze my script with cx-freeze.

So I searched for a few things here and there and found these two pages : here and here. The first one doesn't help me really because my code has nothing to do with pygame or anything alike. The second one is closer to my problem but it seems that he gets the error once his script is frozen, while I get it trying to freeze it. Also, I don't understand very well the answer given to him (concerning the "little script" he would have to run).

So I run everything on Windows 64bits with Python 2.7.8 and Anaconda. I have a little code (running great):

import random as rd
import sqlite3

base=sqlite3.connect("Test_anglais.sqlite")  

def test():
    n=int(input("How many words would you like to review ? \n"))
    for i in range(n):
        rand=rd.randint(1,130)
        recherche=base.execute("SELECT Français FROM vocabulaire WHERE rowid=?", (rand,))
        L=recherche.fetchall()
        question=L[0][0]
        reponse=raw_input("""Please give me the english translation of "%s" : \n""" %question)
        reponse=unicode(reponse)
        recherche=base.execute("SELECT Anglais FROM vocabulaire WHERE rowid=?", (rand,))
        L=recherche.fetchall()
        answer=L[0][0]
        if answer==reponse:
            print(u"Well done !")
            print('\n')
        else :
            print("""You'll need to study a little bit more... The right answer was "%s".""" %answer)
            print("\n")

test()

Nothing complicated here, I just stick out random french words from a database (in utf-8), ask for the translation in english, and see if it maches correctly.

Then I have a steup.py file to freeze it all :

from cx_Freeze import setup, Executable

setup(
    name = "Vocab Test",
    version = "0.1",
    description = "This program will help you review your english vocab",
    executables = [Executable("teste_ton_vocab.py")],
)

But then, when I ask

python.exe setup.py build

I get a awfully long error description, going like this :

running build
running build_exe
creating directory build\exe.win-amd64-2.7
copying C:\Anaconda\lib\site-packages\cx_Freeze\bases\Console.exe -> build\exe.win-amd64-2.7\teste_ton_vocab.exe
copying C:\Anaconda\python27.dll -> build\exe.win-amd64-2.7\python27.dll
Traceback (most recent call last):
  File "setup.py", line 17, in <module>
    executables = [Executable("teste_ton_vocab.py")],
  File "C:\Anaconda\lib\site-packages\cx_Freeze\dist.py", line 362, in setup
    distutils.core.setup(**attrs)
  File "C:\Anaconda\lib\distutils\core.py", line 151, in setup
    dist.run_commands()
  File "C:\Anaconda\lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "C:\Anaconda\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Anaconda\lib\distutils\command\build.py", line 127, in run
    self.run_command(cmd_name)
  File "C:\Anaconda\lib\distutils\cmd.py", line 326, in run_command
    self.distribution.run_command(command)
  File "C:\Anaconda\lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "C:\Anaconda\lib\site-packages\cx_Freeze\dist.py", line 232, in run
    freezer.Freeze()
  File "C:\Anaconda\lib\site-packages\cx_Freeze\freezer.py", line 621, in Freeze
    self._FreezeExecutable(executable)
  File "C:\Anaconda\lib\site-packages\cx_Freeze\freezer.py", line 211, in _FreezeExecutable
    self._AddVersionResource(exe.targetName)
  File "C:\Anaconda\lib\site-packages\cx_Freeze\freezer.py", line 150, in _AddVersionResource
    stamp(fileName, versionInfo)
  File "C:\Anaconda\lib\site-packages\win32\lib\win32verstamp.py", line 157, in stamp
    vs = VS_VERSION_INFO(vmaj, vmin, vsub, vbuild, sdata, vdata, is_debug, is_dll)
  File "C:\Anaconda\lib\site-packages\win32\lib\win32verstamp.py", line 105, in VS_VERSION_INFO
    result = pad32(result) + StringFileInfo(sdata) + VarFileInfo(vdata)
  File "C:\Anaconda\lib\site-packages\win32\lib\win32verstamp.py", line 83, in StringFileInfo
    result = pad32(result) + StringTable('040904E4', data)
  File "C:\Anaconda\lib\site-packages\win32\lib\win32verstamp.py", line 75, in StringTable
    result = result + String(k, v)
  File "C:\Anaconda\lib\site-packages\win32\lib\win32verstamp.py", line 64, in String
    value = nullterm(value)
  File "C:\Anaconda\lib\site-packages\win32\lib\win32verstamp.py", line 50, in nullterm
    return (unicode(s) + u'\0').encode('unicode-internal')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 32: ordinal not in range(128)

So here I am and I really don't know what to do or where it could come from. The line that's getting the error ; line 32 if I get it right it this one :

print("""You'll need to study a little bit more... The right answer was "%s".""" %answer)

So I guess it has something to do with the "answer" I get from the data base. (It's a unicode chain as u'in-company training' for example).

Do you have any idea here ? I tried to be as precise as possible, so sorry for the long message and all...

Thanks for the help !

Aequalis

PS : By the way shouldn't I add somewhere in the setup.py file the database Test_anglais.sqlite ??

Aucun commentaire:

Enregistrer un commentaire