py2exe: UnicodeDecodeError(无法解码字节0xd1)

0 投票
1 回答
1640 浏览
提问于 2025-04-17 12:42

我正在尝试将我的应用程序打包成可在Windows上运行的版本。这个应用程序是用PyQt4开发的,基于Python 2.7构建的。它在我的电脑上编译并运行得很好,但在其他没有安装Python的电脑上就出错了:

 File "quirinus.py", line 4, in <module>
 File "zipextimporter.pyc", line 82, in load_module
 File "bin\core.pyc", line 17, in <module>
 File "bin\xstring.pyc", line 19, in str2unicode
 File "encodings\utf_8.pyc", line 16, in decode
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd1 in position 3: invalid continuation byte

这是我的setup.py文件的代码:

from distutils.core import setup
import py2exe, sys, os
from glob import glob
data_files = [('Microsoft.VC90.CRT', glob(r'..\Microsoft.VC90.CRT\*.*'))]
sys.path.append(r'..\Microsoft.VC90.CRT')
sys.argv.append('py2exe')

py2exe_options = dict(
  includes=['sip',
            'encodings',
            'encodings.ascii',
            'encodings.utf_8',
            'encodings.cp866'],
  excludes=['_ssl', 'pyreadline', 'difflib', 'doctest',
    'tarfile', 'bz2', 'zipfile', 'optparse', 'pickle',
    'pywin', 'pywin.debugger', 'pywin.debugger.dbgcon',
    'pywin.dialogs', 'pywin.dialogs.list',  'calendar',
    'Tkconstants', 'Tkinter', 'tcl', '_gtkagg', '_tkagg',
    'bsddb', 'curses', 'email', 'Tkconstants', 'Tkinter'],
  dll_excludes=['msvcp90.dll', 'msvcr90.dll', 'msvcm90.dll',
    'libgdk-win32-2.0-0.dll', 'libgobject-2.0-0.dll',
    'tcl84.dll', 'tk84.dll'],
  compressed=True
)

setup(
  name='Quirinus',
  author='Dmitriy Selyutin',
  author_email='ghostmansd@google.com',
  description='Quirinus: Dictionary',
  version='0.1',
  windows = \
  [
    {
      'script': 'quirinus.py',
      'icon_resources': [(0, 'icons/icon.ico')]
    }
  ],
  options={'py2exe': py2exe_options},
  zipfile = None,
  data_files = data_files
  )

这是我用来打包的命令:python.exe .\setup.py py2exe -b 1

我的项目中的每个源文件开头都有“编码”这一行:

# coding: UTF-8

我觉得我已经做了所有能让Unicode正常工作的事情。而且在每台有Python的电脑上都能正常运行。:-) 但是在没有Python的电脑上,应用程序就无法启动。有没有人遇到过这个问题?

附注:我也尝试过用PyInstaller打包(python.exe .\pyinstaller.py -F -w),但打包后的应用程序无法运行。是的,我所有的字符串在源代码中都是这样的形式:u'string'。

1 个回答

0

我觉得这个问题和另一台电脑上有没有安装Python没什么关系。你应该再看看那台电脑的处理器规格,跟你自己电脑的规格比一比。

因为程序的运行可能会很依赖处理器,所以在不同处理器之间可能会出现兼容性的问题。

撰写回答