使用 Pyinstaller 时出现“UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 3965: invalid start byte”

时间:2021-07-09 21:40:30

标签: python-3.x utf-8 pyinstaller

我正在尝试从两个 python 脚本创建一个可执行文件。一个脚本定义了另一个后端脚本的 GUI。后端正在读取 excel 文件,使用它们创建 DataFrame 进行操作,然后输出一个新的 excel 文件。这是在 excel 文件中读取的代码,其中“user_path、userAN、userRev1、userRev2”作为用户输入从 GUI 中获取:

import pandas as pd
import numpy as np
import string
from tkinter import messagebox
import os

def generate_BOM(user_path, userAN, userRev1, userRev2):
    ## Append filepath with '/' if it does not include directory separator
    if not (user_path.endswith('/') or user_path.endswith('\\')):
        user_path = user_path + '/'
    ## Set filepath to current directory if user inputted path does not exist
    if not os.path.exists(user_path):
        user_path = '.'
    
    fileFormat1 = userAN + '_' + userRev1 + '.xls'
    fileFormat2 = userAN + '_' + userRev2 + '.xls'
    
    for file in os.listdir(path=user_path):
        if file.endswith(fileFormat1):
            df1 = pd.read_excel(user_path+file, index_col=None) 
        
        if file.endswith(fileFormat2):
            df2 = pd.read_excel(user_path+file, index_col=None)
    

通过 Spyder 运行这两个脚本时,一切正常。要创建 exe,我使用 Pyinstaller 和以下命令:

pyinstaller --onefile Delta_BOM_Creator.py

这会导致以下错误:

Traceback (most recent call last):
  File "c:\users\davhar\anaconda3\lib\runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\davhar\anaconda3\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\davhar\Anaconda3\Scripts\pyinstaller.exe\__main__.py", line 7, in <module>
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 114, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\__main__.py", line 65, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 737, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 684, in build
    exec(code, spec_namespace)
  File "C:\Users\davhar\.spyder-py3\DELTA_BOM_Creator\Delta_BOM_Creator.spec", line 7, in <module>
    a = Analysis(['Delta_BOM_Creator.py'],
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 242, in __init__
    self.__postinit__()
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\building\datastruct.py", line 160, in __postinit__
    self.assemble()
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\building\build_main.py", line 414, in assemble
    priority_scripts.append(self.graph.run_script(script))
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\depend\analysis.py", line 303, in run_script
    self._top_script_node = super(PyiModuleGraph, self).run_script(
  File "c:\users\davhar\anaconda3\lib\site-packages\PyInstaller\lib\modulegraph\modulegraph.py", line 1411, in run_script
    contents = fp.read() + '\n'
  File "c:\users\davhar\anaconda3\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x93 in position 3965: invalid start byte

我已经尝试了所有我能找到的与这个问题有些相关的方法。仅列出几个:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 15: invalid start byte

https://www.dlology.com/blog/solution-pyinstaller-unicodedecodeerror-utf-8-codec-cant-decode-byte/

Pandas read _excel: 'utf-8' codec can't decode byte 0xa8 in position 14: invalid start byte

我从来没有用过 Pyinstaller,也从来没有用 python 创建过可执行文件,所以很抱歉我是个大菜鸟。

解决方案:我找到了解决方案。我进入了错误中提到的 codecs.py 文件,并在第 322 行添加了 'ignore'

(result, consumed) = self.buffer_decode(data, 'ignore', final)

0 个答案:

没有答案