cx_freeze “zlib not avaiable” error when using mul

2019-03-29 21:20发布

问题:

I have a Python application which works perfectly when run through the standard interpreter, but not when frozen with cx_freeze.

My application makes use of the python Threading module, and typically has around 5 components running, each of which can be individually enabled / disabled in its configuration, and each running in their own individual thread.

With 1 or 2 components enabled, no issue. However, when 3 or more components are enabled, I see the following error message for all or almost all of my threads:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python\64-bit\3.3\lib\threading.py", line 637, in _bootstrap_inner
  File "<absolute path to my .py file on disk>", line 21, in run
    from module import screenshot
  File "C:\Python\64-bit\3.3\lib\importlib\_bootstrap.py", line 1616, in _handle_fromlist
  File "C:\Python\64-bit\3.3\lib\importlib\_bootstrap.py", line 313, in _call_with_frames_removed
  File "C:\Python\64-bit\3.3\lib\importlib\_bootstrap.py", line 1567, in _find_and_load
  File "C:\Python\64-bit\3.3\lib\importlib\_bootstrap.py", line 1534, in _find_and_load_unlocked
  File "<absolute path to my .py file on disk>", line 1, in <module>
    from PIL import ImageGrab
  File "C:\Python\64-bit\3.3\lib\importlib\_bootstrap.py", line 1567, in _find_and_load
  File "C:\Python\64-bit\3.3\lib\importlib\_bootstrap.py", line 1534, in _find_and_load_unlocked
zipimport.ZipImportError: can't decompress data; zlib not available

A few things to note:

  • Python is not installed at C:\Python\64-bit\3.3, so I'm not sure why this path appears in the output above. It is installed in its default location of C:\Python33. (Not sure if the path shown above is a dummy one, or maybe the path of the person who compiled one of my dependencies.)
  • To reiterate - this starts to constantly happen when I start up more than 2 threads or so. It never happens with just 1 active thread, and I have never seen it happen with 2 active threads.
  • This issue only happens when cx_freeze is used. Running it with the Python interpreter produces no errors. I've tried a few different setup.py directives (e.g. appendScriptToLibrary, appendScriptToExe, copyDependentFiles, create_shared_zip), but the issue is always the same.
  • My code doesn't make use of zip functions - so this must be something specific to the freezing process.

My thoughts so far:

  • I think there is some issue with a thread trying to access the zlib or zipimport modules when another thread is using them (i.e. has a lock on them). (Interesting that this doesn't happen for 2 threads though.) My Google-ing has made me do some research into the GIL - could this be an issue here? Or, is there any way to allow the threads to only grab a shared lock on a file instead of an exclusive lock?
  • Being a relative newbie with cx_freeze, I've tried a few different methods of including the zlib module in setup.py. However, I don't think that this is an issue with zlib specifically, since it runs fine outside of cx_freeze.

Environment:

  • Windows 7 64-bit
  • Python 3.3 64-bit
  • cx_freeze 4.3.2

See http://www.filedropper.com/threadtest for a reproduction of this issue with minimal code.