简体   繁体   English

Python:使用pyinstaller,cx_freeze或p2exe编译为exe时,ftfy导致应用程序崩溃

[英]Python: ftfy causes app crash when compiled to exe using pyinstaller, cx_freeze or p2exe

Whenever I import ftfy , and use it in my python script apps, I have no problems at all. 每当我导入ftfy ,并在我的python脚本应用程序中使用它时,我都没有任何问题。

If I compile to binary exe using pyinstaller , cx_freeze or py2exe , my app will compile without problems, but crashes every time time when I run the app. 如果我使用pyinstallercx_freezepy2exe编译为二进制exe,则我的应用程序将毫无问题地进行编译,但是每次运行该应用程序时都会崩溃。

The reason it crashes is because it cannot find the char_classes.dat file from chardata.py . 它崩溃的原因是因为它无法从chardata.py找到char_classes.dat文件。

Here is a typical output that I get when I run my app after compiling with pyinstaller and it crashes: 这是使用pyinstaller编译后运行我的应用程序时崩溃的典型输出:

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site- 文件“ C:\\ WinPy34 \\ python-3.4.3.amd64 \\ Lib \\ site-
packages\\PyInstaller\\loader\\pyimod03_importers.py", line 363, in load_module exec(bytecode, module. dict ) 包\\ PyInstaller \\装载机\\ pyimod03_importers.py”,线363,在load_module EXEC(字节码,模块。 字典

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-packages\\ftfy\\chardata.py", line 141, in CHAR_CLASS_STRING = zlib.decompress(resource_string( name , 'char_classes.dat')).decode('ascii') 在CHAR_CLASS_STRING = zlib.decompress(resource_string( name ,'char_classes.dat'))中的文件141行中的文件“ C:\\ WinPy34 \\ python-3.4.3.amd64 \\ Lib \\ site-packages \\ ftfy \\ chardata.py”。解码('ascii')

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-packages\\pkg_resources__init__.py", line 1173, in resource_string self, resource_name 文件“ C:\\ WinPy34 \\ python-3.4.3.amd64 \\ Lib \\ site-packages \\ pkg_resources__init __。py”,行1173,位于resource_string自我,resource_name中

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-packages\\pkg_resources__init__.py", line 1605, in get_resource_string 文件“ C:\\ WinPy34 \\ python-3.4.3.amd64 \\ Lib \\ site-packages \\ pkg_resources__init __。py”在get_resource_string中的第1605行

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

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-packages\\pkg_resources__init__.py", line 1683, in _get return self.loader.get_data(path) File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-packages\\PyInstaller\\loader\\pyimo d03_importers.py", line 445, in get_data with open(path, 'rb') as fp: 文件``C:\\ WinPy34 \\ python-3.4.3.amd64 \\ Lib \\ site-packages \\ pkg_resources__init __。py'',行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')为fp:

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

d2e returned -1 d2e返回-1

This output says that it cannot find the char_classes.dat file even though that file is there -- as plain as day -- within the ftfy module in the site-packages directory. 此输出表明,即使该文件存在于site-packages目录中的ftfy模块中,也无法找到char_classes.dat文件(就像平常一样)。

According to your log, the file looked for is C:\\Users\\BILLTH~1\\AppData\\Local\\Temp\\_MEI64282\\ftfy\\char_classes.dat . 根据您的日志,查找的文件是C:\\Users\\BILLTH~1\\AppData\\Local\\Temp\\_MEI64282\\ftfy\\char_classes.dat

As it is a temporary directory, I guess that it is where the archive was extracted. 由于它是一个临时目录,所以我想它是提取存档的位置。 These compiler tools create an auto-extractable zip archive which extracts the sources and the embeded python interpreter in a temporary folder, sets up some environment variables, and runs python with the right parameters. 这些编译器工具会创建一个可自动提取的zip归档文件,该文件可将源文件和python解释器提取到一个临时文件夹中,设置一些环境变量,并使用正确的参数运行python。

Therefore, I guess that your char_classes.dat file was not embeded by the tool, probably because the tool doesn't see the dependency. 因此,我猜您的char_classes.dat文件没有被该工具嵌入,可能是因为该工具没有看到依赖关系。

Using pyinstaller, you will probably find the section of the documentation Using Data Files from a Module interesting, and add the following argument to your Analysis call: 使用pyinstaller,您可能会发现文档“ 使用模块中的数据文件 ”部分很有趣,并将以下参数添加到Analysis调用中:

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

I've found a limited solution to this issue. 我已经找到了解决此问题的有限方法。

For me, I was unable to get Using Data Files from a Module to work. 对我来说,我无法从“模块中使用数据文件”正常工作。 I also had no luck with any kind of data specification from a hook file. 我也对钩子文件中的任何数据规范都不走运。

However, it worked using a relative path from my .spec file (which is automatically created in the working directory where you run PyInstaller). 但是,它使用我的.spec文件(在运行PyInstaller的工作目录中自动创建)的相对路径来工作。

In my case, 就我而言

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)

The important line being 重要的是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM