Search code examples
pythonkivypyinstaller

How can I make PyInstaller understand the encoding of my CSV file?


I have a finished Kivy app where everything works as intended, in this GitHub repository.

Now I'm trying to use PyInstaller to build an .exe (not a standalone file, but a folder).

This is the PyInstaller .spec file:

a = Analysis(
    ['D:\\Documents\\PyCharm projects\\PyCharm projects\\DesktopApp\\SaniStore_logo.ico', 'D:\\Documents\\PyCharm projects\\PyCharm projects\\DesktopApp\\main.py'],
    pathex=[],
    binaries=[],
    datas=[
    ("D:\Documents\PyCharm projects\PyCharm projects\DesktopApp\Components_data.csv", "."),],
    hiddenimports=[],
    hookspath=[],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    noarchive=False,
    decode=False
)
pyz = PYZ(a.pure)
a.datas += [('Code\sanistore.kv','D:\\Documents\\PyCharm projects\\PyCharm projects\\DesktopApp\\sanistore.kv','DATA')]
exe = EXE(
    pyz,
    a.scripts,
    [],
    exclude_binaries=True,
    name='SaniStore',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)
coll = COLLECT(
    exe,
    Tree('D:\\Documents\\PyCharm projects\\PyCharm projects\\DesktopApp\\'),
    a.binaries,
    a.datas,
    *[Tree(p) for p in (sdl2.dep_bins + glew.dep_bins)],
    strip=False,
    upx=True,
    upx_exclude=[],
    name='SaniStore',
)

When I tried to create the .exe, I got a UnicodeDecodeError with this traceback:

Traceback (most recent call last):
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\__main__.py", line 205, in _console_script_run
    run()
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\__main__.py", line 189, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\__main__.py", line 61, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\building\build_main.py", line 1042, in main
    build(specfile, distpath, workpath, clean_build)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\building\build_main.py", line 982, in build
    exec(code, spec_namespace)
  File "SaniStore.spec", line 5, in <module>
    a = Analysis(
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\building\build_main.py", line 468, in __init__
    self.__postinit__()
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\building\datastruct.py", line 184, in __postinit__
    self.assemble()
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\building\build_main.py", line 606, in assemble
    priority_scripts.append(self.graph.add_script(script))
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\depend\analysis.py", line 268, in add_script
    self._top_script_node = super().add_script(pathname)
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1246, in add_script
    contents = fp.read() + '\n'
  File "C:\Users\danie\AppData\Local\Programs\Python\Python310\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb0 in position 14: invalid start byte

My understanding is that the problem has something to do with characters somewhere in my code that the UTF-8 doesn't accept.

I found many ideas about how to solve the problem, but none of them worked for me. Most of them were related to my .csv database.

For example I changed my code accordingly from:

main_script_directory = os.path.abspath(os.path.dirname(__file__))
csv_file_path = os.path.join(main_script_directory, "Components_data.csv")
self.df = pd.read_csv(csv_file_path, encoding='utf-8')

to:

with open(csv_file_path, 'rb') as f:
    contents = f.read()
self.df = pd.read_csv(io.BytesIO(contents), encoding='utf-8')

But it did not fix the problem. I also tried changing the encoding format to 'utf-8-sig', 'latin1' etc. without any change.

What is wrong, and how do I fix it?


Solution

  • You are including the the icon file at the start of the spec file where python scripts are expected. So Pyinstaller is trying to analyze the icon as a python script. Remove the icon file from that list and include it in the datas list.