美文网首页
解决Gooey在打包成exe文件后打印中文报UnicodeDec

解决Gooey在打包成exe文件后打印中文报UnicodeDec

作者: idealCity | 来源:发表于2020-01-06 18:15 被阅读0次

    问题

    在使用Gooey这个工具生成GUI的时候,没有打包前测试是好的,但是当打包成exe文件后,双击exe运行填入所需选项执行报UnicodeDecodeError: 'utf-8' codec can't decode的错误。

    PS C:\Users\faces\Desktop\gooey demo\dist> .\auto.exe
    Exception in thread Thread-1:
    Traceback (most recent call last):
      File "threading.py", line 926, in _bootstrap_inner
      File "threading.py", line 870, in run
      File "site-packages\gooey\gui\processor.py", line 71, in _forward_stdout
      File "site-packages\gooey\gui\processor.py", line 84, in _extract_progress
    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 13: invalid start byte
    

    通过查找github中提交的issue发现是打包后环境的encoding与打包时的encoding不一致导致的问题。

    解决方案

    在Gooey装饰器中加入关键字参数encoding='cp936'

    from gooey import Gooey, GooeyParser
    @Gooey(encoding='cp936')
    def main():
        parser = GooeyParser(description="Export music lite") 
        parser.add_argument('exe文件', widget="FileChooser")
        parser.add_argument('flac文件夹', widget="DirChooser")
        parser.add_argument('MP3导出文件夹', widget="DirChooser")
        args = parser.parse_args()
        print(args)
    if __name__ == "__main__":
        main()
    

    问题

    接下来我想更改Gooey生成的GUI页面为中文,那么代码改为

    
    from gooey import Gooey, GooeyParser
    @Gooey(encoding='cp936', language='chinese')
    def main():
        parser = GooeyParser(description="Export music lite") 
        ...
    

    这时候执行会报如下错误

    PS C:\Users\faces\Desktop\gooey demo\dist> .\auto.exe
    Traceback (most recent call last):
      File "auto.py", line 17, in <module>
      File "site-packages\gooey-1.0.3-py3.7.egg\gooey\python_bindings\gooey_decorator.py", line 87, in inner2
      File "auto.py", line 12, in main
      File "site-packages\gooey-1.0.3-py3.7.egg\gooey\python_bindings\gooey_parser.py", line 114, in parse_args
      File "site-packages\gooey-1.0.3-py3.7.egg\gooey\python_bindings\gooey_decorator.py", line 82, in run_gooey
      File "site-packages\gooey-1.0.3-py3.7.egg\gooey\gui\application.py", line 21, in run
      File "site-packages\gooey-1.0.3-py3.7.egg\gooey\gui\application.py", line 28, in build_app
      File "site-packages\gooey-1.0.3-py3.7.egg\gooey\gui\lang\i18n.py", line 24, in load
      File "json\__init__.py", line 293, in load
    UnicodeDecodeError: 'gbk' codec can't decode byte 0xa7 in position 20: illegal multibyte sequence
    

    解决方案

    从报错信息中可以看到是因为load代码的时候使用encoding='cp936'去加载文件导致的错误,那么简单粗暴的方法是编辑site-packages\gooey-1.0.3-py3.7.egg\gooey\gui\lang\i18n.py这个文件

    找到

        with io.open(os.path.join(language_dir, json_file), 'r', encoding=encoding) as f:
          _DICTIONARY = json.load(f)
    

    修改为

        with io.open(os.path.join(language_dir, json_file), 'r', encoding='utf-8') as f:
          _DICTIONARY = json.load(f)
    

    相关文章

      网友评论

          本文标题:解决Gooey在打包成exe文件后打印中文报UnicodeDec

          本文链接:https://www.haomeiwen.com/subject/dzgkactx.html