简体   繁体   中英

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.

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.

The reason it crashes is because it cannot find the char_classes.dat file from chardata.py .

Here is a typical output that I get when I run my app after compiling with pyinstaller and it crashes:

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-
packages\\PyInstaller\\loader\\pyimod03_importers.py", line 363, in load_module exec(bytecode, module. dict )

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')

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-packages\\pkg_resources__init__.py", line 1173, in resource_string self, resource_name

File "C:\\WinPy34\\python-3.4.3.amd64\\Lib\\site-packages\\pkg_resources__init__.py", line 1605, in get_resource_string

return 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:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\BILLTH~1\\Ap pData\\Local\\Temp\\_MEI64282\\ftfy\\char_classes.dat'

d2e returned -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.

According to your log, the file looked for is 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.

Therefore, I guess that your char_classes.dat file was not embeded by the tool, probably because the tool doesn't see the dependency.

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:

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).

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")
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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