Search code examples
pythonzlibcx-freeze

zlib not available in a cx frozen python 3.4 application


When I compile my python 3.4 application into an executable with cx_freeze on Windows 10 64-bit (32-bit python) I get the following run time exception intermittently (about 1 time in 4) from seemingly random import attempts (eg import getpass / import QtCore):

zipimport.ZipImportError: can't decompress data; zlib not available

When I don't get the error on startup the application runs fine. Some notes:

1- I do not get the exception with similar applications built using python 2.7.

2- I do not get the exception when running directly from the .pyw file (ie unfrozen).

Trace:

File "c:\python\32-bit\3.4\lib\threading.py", line 921, in _bootstrap_inner
File ".\tempCheckout\CanPy\CanAsync.py", line 400, in run
File ".\tempCheckout\CanPy\CanAsync.py", line 483, in _connect
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2237, in _find_and_load
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2226, in _find_and_load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1191, in _load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1161, in _load_backward_compatible
File ".\tempCheckout\CanPy\CanSsh.py", line 1, in <module>
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2237, in _find_and_load
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2226, in _find_and_load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1191, in _load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1161, in _load_backward_compatible
File ".\tempCheckout\CanPy\CommUtil\SshTunnel.py", line 1, in <module>
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2237, in _find_and_load
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 2226, in _find_and_load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1191, in _load_unlocked
File "c:\python\32-bit\3.4\lib\importlib\_bootstrap.py", line 1161, in _load_backward_compatible
zipimport.ZipImportError: can't decompress data; zlib not available

Also, I found this which seems related: cx_freeze "zlib not avaiable" error when using multiple threads

Think that my python 3.4 application is using two external libraries that are dependent on zlib and that there is a lock around its usage. These libraries are not in python 2.7 and thus I don't see the issue. If I can stop these libraries loading asynchronously then I might fix the issue.


Solution

  • OK, my hypothesis seems correct. The library was being loaded in two threads. Waiting until the first thread had imported before the second seems to have fixed the issue. (although maybe I could have just changed the relative timings but I'll update if this turns out to be the case). In response to the notes:

    1- python 2.7 application wasn't loading zlib, so this is a red herring.

    2- I think the timings must have been such in the .pyw that the issue did not occur.