distroless icon indicating copy to clipboard operation
distroless copied to clipboard

Python3 images does not provide .pyc files

Open ganguin opened this issue 4 years ago • 1 comments

The python images do not provide compiled form of .py files which result in image size increase on startup and longer startup times.

All .py could be compiled in their .pyc form:

python /usr/lib/python3.9/compileall.py -f /usr/lib/python3.9/

To go a step further .py files could be removed leaving only the .pyc files. However, PEP 3147 changed the .pyc files path to be under the pycache directory. Removing the .py won't work with PEP 3147. But compileall has a legacy flag (-b) to ignore PEP 3147:

python /usr/lib/python3.9/compileall.py -b -f /usr/lib/python3.9/

Doing so, all .py files could be deleted.

ganguin avatar Oct 12 '21 22:10 ganguin

This is a bit tricky to implement. The compiled Python bytecode files depend on the exact version of the Python interpreter, I believe. This means we would need to compile the bytecode using the exact version of Python that is shipped with the container. I think this may be possible with the relatively recently added Bazel Docker run rules? https://github.com/bazelbuild/rules_docker/blob/master/docker/util/README.md

evanj avatar Oct 22 '21 15:10 evanj