Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python plugin. Python is broken in core24 classic snaps if snap is used on an older Ubuntu LTS version #786

Open
NucciTheBoss opened this issue Jul 17, 2024 · 6 comments
Labels
Bug Something isn't working

Comments

@NucciTheBoss
Copy link

NucciTheBoss commented Jul 17, 2024

Bug Description

Python is broken in core24 classic snaps when the snap is installed on an older Ubuntu base. The embedded Python interpreter in the squashfs archive borks when you try to invoke a Python script. Rather than running the script, Python will throw platform related errors such as not finding the encodings module.

The Python interpreter will work properly if using the core24 classic snap on Ubuntu 24.04 LTS, but will fail if you use the classic snap on an older LTS release such as 22.04 or 20.04.

Related issues and threads

To Reproduce

For the Slurm snap, I have already reverted back to core22 for the time being, however, there was time when we were publishing core24-base Slurm snaps with this issue. Use the following commands to reproduce the issue:

# First, build version of the Slurm snap that has this issue.
$ git clone [email protected]:charmed-hpc/slurm-snap.git 
$ cd slurm-snap
$ git checkout 23.11.7/rev441/amd64
$ snapcraft -v pack

# Now with the built snap, use LXD to reproduce the issue
$ lxc launch ubuntu:jammy python-tester --vm
$ lxc file push slurm_*.snap python-tester/root/slurm.snap
$ lxc exec python-tester -- snap install ./slurm.snap --dangerous --classic

# See Python platform error be printed.

part yaml

# The current Slurm snap has changed a bit from when we reverted from core24, so here's the link
# to the revision with problematic part: https://github.com/charmed-hpc/slurm-snap/blob/a3ab81f8c67d0b15ee17c3c62ec2a9850005b3de/snap/snapcraft.yaml#L145-L160

  hooks:
    source: .
    build-attributes: [enable-patchelf]
    plugin: python
    build-packages:
      - git
      - python3-setuptools
    python-requirements: [requirements.txt]
    stage-packages:
      - libpython3.12-stdlib
      - libpython3.12-minimal
      - python3-venv
      - python3.12-minimal
    override-build: |
      craftctl default
      snap-helpers write-hooks

Relevant log output

# From the Slurm snap

error: cannot perform the following tasks:
- Run install hook of "slurm" snap if present (run hook "install":
-----
Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Python path configuration:
  PYTHONHOME = (not set)
  PYTHONPATH = (not set)
  program name = 'python3'
  isolated = 0
  environment = 1
  user site = 1
  safe_path = 0
  import site = 1
  is in build tree = 0
  stdlib dir = '/usr/lib/python3.12'
  sys._base_executable = '/snap/slurm/417/usr/bin/python3.12'
  sys.base_prefix = '/usr'
  sys.base_exec_prefix = '/usr'
  sys.platlibdir = 'lib'
  sys.executable = '/snap/slurm/417/bin/python3'
  sys.prefix = '/usr'
  sys.exec_prefix = '/usr'
  sys.path = [
    '/usr/lib/python312.zip',
    '/usr/lib/python3.12',
    '/usr/lib/python3.12/lib-dynload',
  ]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f75abe3d300 (most recent call first):
  <no Python frame>
-----)

# From certbot after patching PYTHONHOME and PYTHONPATH

AttributeError: partially initialized module 'charset_normalizer' has no attribute 'md__mypyc' (most likely due to a circular import)
@tigarmo
Copy link
Contributor

tigarmo commented Jul 29, 2024

Thanks for the report. It's probably something that we'll need to fix in Snapcraft itself as it's very specific to how snaps are laid out.
Cross-posting a possible fix: @NucciTheBoss can you try this fix and report back?

@upils
Copy link

upils commented Jul 30, 2024

Here is an example of python3.12-based snap working on 18.04, 20.04 and 24.04

@lengau
Copy link
Contributor

lengau commented Aug 17, 2024

What's the shebang on that hook? I'm wondering if it's pointing to the system Python rather than the staged python

@mcw-work
Copy link

mcw-work commented Sep 3, 2024

I'm having the same issue here trying to update the Landscape-client snap to a Core24 base and then installing to a 24.04 instance. Also, the link from @upils is now dead so I can't attempt that one.

The initial issue was a the configure script being unable to find its own modules. This was fixed by setting PYTHONHOME to $SNAP but that now leads to exactly the same log output as @NucciTheBoss.

@tigarmo
Copy link
Contributor

tigarmo commented Sep 3, 2024

The original issue was (supposedly) fixed in Snapcraft 8.3.2, can you open a new issue in Snapcraft with details on the snap and the error log?

@mcw-work
Copy link

mcw-work commented Sep 3, 2024

Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants