py2app icon indicating copy to clipboard operation
py2app copied to clipboard

build_app attempts to copy zlib and it doesn't need to with Python3.11

Open sheffler opened this issue 3 months ago • 2 comments

When running py2app on Mac with a Python3.11 installed in a venv by 'uv', my build fails with the following

  File "/Users/sheffler/git/kivy_osx/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 1217, in _run
    self.run_normal()
  File "/Users/sheffler/git/kivy_osx/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 1330, in run_normal
    self.create_binaries(py_files, pkgdirs, extensions, loader_files)
  File "/Users/sheffler/git/kivy_osx/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 1687, in create_binaries
    dst = self.build_executable(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/sheffler/git/kivy_osx/.venv/lib/python3.11/site-packages/py2app/build_app.py", line 2508, in build_executable
    self.copy_file(zlib.__file__, os.path.dirname(arcdir))
                   ^^^^^^^^^^^^^
AttributeError: module 'zlib' has no attribute '__file__'. Did you mean: '__name__'?

In build_app.py there are these lines:

        if sys.version_info[0] != 2:
            import zlib

            self.copy_file(zlib.__file__, os.path.dirname(arcdir))

If I copy out the block of code above, the build works fine and the executable runs as well. The reason seems to be that the version of python I am using in the .venv is Python3.11 and 'zlib' seems to be a built in

$ python
Python 3.11.11 (main, Dec  6 2024, 21:09:50) [Clang 18.1.8 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> dir(zlib)
['DEFLATED', 'DEF_BUF_SIZE', 'DEF_MEM_LEVEL', 'MAX_WBITS', 'ZLIB_RUNTIME_VERSION', 'ZLIB_VERSION', 'Z_BEST_COMPRESSION', 'Z_BEST_SPEED', 'Z_BLOCK', 'Z_DEFAULT_COMPRESSION', 'Z_DEFAULT_STRATEGY', 'Z_FILTERED', 'Z_FINISH', 'Z_FIXED', 'Z_FULL_FLUSH', 'Z_HUFFMAN_ONLY', 'Z_NO_COMPRESSION', 'Z_NO_FLUSH', 'Z_PARTIAL_FLUSH', 'Z_RLE', 'Z_SYNC_FLUSH', 'Z_TREES', '__doc__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 'adler32', 'compress', 'compressobj', 'crc32', 'decompress', 'decompressobj', 'error']
>>> zlib.__loader__
<class '_frozen_importlib.BuiltinImporter'>
>>> 

IN the system's python3.10, it is loaded from a file

$ /usr/bin/python3
Python 3.9.6 (default, Apr 30 2025, 02:07:17) 
[Clang 17.0.0 (clang-1700.0.13.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import zlib
>>> zlib.__file__
'/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/lib-dynload/zlib.cpython-39-darwin.so'
>>> 

Not sure how to handle this, perhaps it is related to the python that uv installed. Reporting it here regardless.

sheffler avatar Sep 20 '25 16:09 sheffler

Simple workaround is to use py3.10 in the project $ uv venv --python=3.10

sheffler avatar Sep 20 '25 16:09 sheffler

Considering there's an open discussion about forcing Python to require zlib, I don't think this is purely about the Python version, but rather about how you installed one version vs another.

IN the system's python3.10, it is loaded from a file

$ /usr/bin/python3
Python 3.9.6 (default, Apr 30 2025, 02:07:17) 

Also, this isn't quite right - you say 3.10, but clearly it's actually 3.9.

radarhere avatar Sep 25 '25 12:09 radarhere