【问题标题】:Python: ftfy causes app crash when compiled to exe using pyinstaller, cx_freeze or p2exePython:使用 pyinstaller、cx_freeze 或 p2exe 编译为 exe 时,ftfy 会导致应用程序崩溃
【发布时间】:2016-05-31 01:51:21
【问题描述】:

每当我导入 ftfy 并在我的 python 脚本应用程序中使用它时,我都没有任何问题。

如果我使用pyinstallercx_freezepy2exe 编译为二进制 exe,我的应用程序将毫无问题地编译,但每次运行应用程序时都会崩溃。

它崩溃的原因是它无法从chardata.py 中找到char_classes.dat 文件。

这是我在使用 pyinstaller 编译后运行我的应用程序并崩溃时得到的典型输出:

文件“C:\WinPy34\python-3.4.3.amd64\Lib\site-
包\PyInstaller\loader\pyimod03_importers.py",第 363 行,在 load_module exec(bytecode, module.dict)

文件 "C:\WinPy34\python-3.4.3.amd64\Lib\site-packages\ftfy\chardata.py", 第 141 行,在 CHAR_CLASS_STRING = zlib.decompress(resource_string(名称, 'char_classes.dat')).decode('ascii')

文件 "C:\WinPy34\python-3.4.3.amd64\Lib\site-packages\pkg_resources__init__.p y",第 1173 行,在 resource_string self,resource_name

文件 "C:\WinPy34\python-3.4.3.amd64\Lib\site-packages\pkg_resources__init__.p y",第 1605 行,在 get_resource_string 中

返回 self._get(self._fn(self.module_path, resource_name))

文件 "C:\WinPy34\python-3.4.3.amd64\Lib\site-packages\pkg_resources__init__.p y",第 1683 行,在 _get 中 返回 self.loader.get_data(path) 文件 "C:\WinPy34\python-3.4.3.amd64\Lib\site-packages\PyInstaller\loader\pyimo d03_importers.py",第 445 行,在 get_data 中,open(path, 'rb') as fp:

FileNotFoundError: [Errno 2] 没有这样的文件或目录: 'C:\Users\BILLTH~1\Ap pData\Local\Temp\_MEI64282\ftfy\char_classes.dat'

d2e 返回 -1

此输出表明它无法找到 char_classes.dat 文件,即使该文件在 site-packages 目录中的 ftfy 模块中也存在 - 就像白天一样。

【问题讨论】:

  • 我知道这是旧的,但想知道您是否找到了解决方案?

标签: python python-3.x pyinstaller py2exe cx-freeze


【解决方案1】:

根据您的日志,查找的文件是C:\Users\BILLTH~1\AppData\Local\Temp\_MEI64282\ftfy\char_classes.dat

因为它是一个临时目录,我猜它是解压存档的地方。这些编译器工具会创建一个可自动提取的 zip 存档,它将源代码和嵌入的 python 解释器提取到一个临时文件夹中,设置一些环境变量,并使用正确的参数运行 python。

因此,我猜你的char_classes.dat文件没有被工具嵌入,可能是因为工具没有看到依赖。

使用 pyinstaller,您可能会发现文档 Using Data Files from a Module 的部分很有趣,并将以下参数添加到您的 Analysis 调用中:

datas=[('ftfy\char_classes.dat', 'ftfy')]

【讨论】:

  • @madprog...感谢您的意见。我已经尝试过更改 Pyinstaller 规范文件中的数据和二进制文件。它没有用。 char_classes.dat 文件是一个二进制文件。
【解决方案2】:

我找到了解决这个问题的有限方法。

对我来说,我无法让Using Data Files from a Module 工作。对于挂钩文件中的任何类型的数据规范,我也没有运气。

但是,它使用我的 .spec 文件中的相对路径(在运行 PyInstaller 的工作目录中自动创建)。

就我而言,

a = Analysis(['ShellClient.py'],
             pathex=['C:\\workspaces\\ScoutSheet\\ScoutSheet\\ScoutSheet.Parsers.NERParsers'],
             binaries=[],
             datas=[
                ("./env/Lib/site-packages/ftfy/char_classes.dat", "ftfy")
             ],
             hiddenimports=["ftfy"],
             hookspath=['.\\PyinstallerHooks'],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

重要的线是

datas=[
    ("./env/Lib/site-packages/ftfy/char_classes.dat", "ftfy")
],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-16
    • 2022-12-11
    • 1970-01-01
    • 2011-11-10
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多