Python 3.11 in Snap

Hey, after reading quite a few entries in this forum [1][2][3] & more and a lot of trial-and-error, this is what I have in my core20-based snap to build python3.11 and use it in another part that requires python:

parts:
  python:
    plugin: autotools
    source: https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
    autotools-configure-parameters:
      - --enable-optimizations
      - --prefix=/usr
    build-packages:
      - build-essential
      - gdb
      - lcov
      - pkg-config
      - libbz2-dev
      - libffi-dev
      - libgdbm-dev
      - libgdbm-compat-dev
      - liblzma-dev
      - libncurses5-dev
      - libreadline6-dev
      - libsqlite3-dev
      - libssl-dev
      - lzma
      - lzma-dev
      - tk-dev
      - uuid-dev
      - zlib1g-dev
    stage-packages:
      - libfontconfig1
      - libfreetype6
      - libgdbm-compat4
      - libgdbm6
      - libpng16-16
      - libtcl8.6
      - libtk8.6
      - libx11-6
      - libxau6
      - libxcb1
      - libxdmcp6
      - libxext6
      - libxft2
      - libxrender1
      - libxss1
    prime:
      - -etc
      - -lib/systemd
      - -usr/bin/2to3
      - -usr/bin/2to3-3.11
      - -usr/bin/deb-systemd-helper
      - -usr/bin/deb-systemd-invoke
      - -usr/bin/easy_install-3.11
      - -usr/bin/idle3
      - -usr/bin/idle3.11
      - -usr/bin/pydoc3
      - -usr/bin/pydoc3.11
      - -usr/bin/python3.11-config
      - -usr/bin/python3-config
      - -usr/bin/uuidgen
      - -usr/include
      - -usr/lib/*.a
      - -usr/lib/pkgconfig
      - -usr/lib/python3.11/test
      - -usr/sbin
      - -usr/share
      - -var
  fiduswriter:
    plugin: python
    after: [python]
    python-packages:
        ...
    build-environment:
      - SNAPCRAFT_PYTHON_INTERPRETER: python3.11
      - C_INCLUDE_PATH: $SNAPCRAFT_STAGE/usr/include/python3.11/
    stage:
      - -bin/python
      - -bin/python3
      - -bin/python3.11
    ...
...
apps:
  createsuperuser:
    command: 'bin/manage.py createsuperuser'
    environment:
      PYTHONPATH: $SNAP/lib/python3.11/site-packages
...

The entire package is open source and can be found here [4].

I am not entirely sure why the package that uses python creates three dead links (python, python3 and python3.11) in the bin-folder, but they are not needed as the excuatbles are in the usr/bin folder.

This seems to be working and I thought others might find it useful.

[1] Build a snap with any version of Python I want

[2] How do I correctly install a custom python version and a local wheel package?

[3] How to use an old Python version

[4] https://github.com/fiduswriter/fiduswriter-snap/blob/main/snap/snapcraft.yaml

4 Likes