Byte-compile Python code?

Is it desirable to byte-compile the Python code in a snap package? If so, then how?

Funny you should ask that, since I just added this to our snap!

Here’s the basic pattern we used:

    override-prime: |
      snapcraftctl prime
      /snap/core/current/usr/bin/python3 -m compileall -q -j0 ./path/to/your/python
2 Likes

Yeah, snapcraft itself does it like this.

2 Likes

Thanks, I know I read it somewhere, just can’t find it when I really need it.

1 Like

My override-prime scriptlet failed with 1 after adding the code, it appears that the compilation process failed due to a Python 3 incompatible example code in the prime tree (usr/share/doc/git/contrib/fast-import/import-zips.py).

Here’s my final implementation:

      # This is the last step, let's now compile all our pyc files.
      # 0 --workers: Use all available CPU threads
      "${SNAPCRAFT_PRIME}"/usr/bin/python3 \
        -m compileall \
        -qq \
        --workers 0 \
        "${SNAPCRAFT_PRIME}" \
        || true # May fail on Python 3 incompatible code
1 Like

@Lin-Buo-Ren nice find. Are the examples required in the snap, or are they just taking up space? You might consider filtering them out, which would have the side effect of making this step a bit easier.

1 Like