python-build-standalone
python-build-standalone copied to clipboard
Questions about libpython size, specifically on Linux x86_64
Several possibly-related questions. Note that I'm not necessarily saying anything is "wrong" here, I'm just trying to understand the differences.
So I've noticed two things about libpython.3.xx.so.1.0, working on x86_64-unknown-linux-gnu:
- Python 3.12's libpython is 4x the size of Python 3.11: 212MB vs. 58MB. If you
stripboth of them, the size difference largely vanishes; Python 3.12 is 25.3MB, where Python 3.11 is 24.4MB. - python-build-standalone's stripped libpython is still something like 4x larger than the libpython that comes with Fedora 40, which is 6.1MB for Python 3.12 and 4.9MB for Python 3.11.
This is particularly noticeable when developing standalone binaries with PyInstaller. I've hacked PyInstaller locally to work with python-build-standalone, but the resulting binaries are nearly 3x the size compared to building with Fedora's python even if I build with the stripped libpython, which is kind of rough.
- Python 3.12's libpython is 4x the size of Python 3.11: 212MB vs. 58MB. If you
stripboth of them, the size difference largely vanishes; Python 3.12 is 25.3MB, where Python 3.11 is 24.4MB.
Thanks for this information. I was wondering why my PyInstaller-packaged APP became much larger. 😄
See also related discussion here.
See also related discussion here.
Thanks for this information. I do have a script for installation so I can add a strip step during installing. Currently I just use strip without any CLI flags. Do you have recommended flags?
My installer uses strip -p --strip-unneeded.
I wonder if the debug symbols size has something to do with BOLT, which is 3.12+ only. Maybe BOLT is producing sub-optimal DWARF records, not preserving compression in symbols, or something along those lines.
FWIW on macOS, I only see a ~12% reduction in libpython3.10.dylib size.
I also see that if I install python using UV, the sizes are quite a bit bigger than if I use python slim in docker. Note that the libpython3.13.so is almost 6x larger in UV (python-build-standalone) than in slim. I don't have an option in UV to install the stripped version maybe? If so I will make an issue there. But it also seems that this packs tcl, tk and some other things which I guess are optional as they aren't packed in -slim. So could we make a minimized python standalone for docker case? I know that it will be larger than python:x-slim base images, but I'm fine with that, because using UV is very convenient and universal when using different base images. But currently the difference in size is 78.1MB (slim) vs 120MB (ubuntu24+python-by-uv), so almost a third. Would be good to get closer, and it seems we could get there if .so's where similar size.
Some Pyinstaller comments upstream that might be relevant
- https://github.com/pyinstaller/pyinstaller/pull/9124#issuecomment-2870191061
- https://github.com/pyinstaller/pyinstaller/pull/9124#issuecomment-2870289524
... and the bloat factor due to PyInstaller being unable to drop large unneeded extension modules.