apt.cache.FetchFailedException: E:Method http has died unexpectedly!

I am trying to have a fixed version of Python 3 inside a snap, and was bitten by the problem at “Classic snaps failing on Ubuntu 17.10”.

So I upgraded to the latest snapcraft with snap install snapcraft --beta --classic, and tried building it with:

    command: bin/python3.6

    source: https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
    plugin: autotools
      - --prefix=/usr
      - libssl-dev
      - -usr/include

It started complaining that it couldn’t find prime/bin/python3.6, so I removed the --prefix=/usr bit.

Then it started complaining that it couldn’t find libc6:

/snap/my-snap-name/x1/bin/python3.6: /lib/x86_64-linux-gnu/libc.so.6: version `GLIBC_2.25' not found (required by /snap/my-snap-name/x1/bin/python3.6)

So I added stage-packages: [libc6].

But now I’m stuck with a apt.cache.FetchFailedException:

$ snapcraft
Preparing to pull python3
Fetched 0 B in 0s (0 B/s)
Traceback (most recent call last):
  File "/snap/snapcraft/876/lib/python3.6/site-packages/apt/cache.py", line 452, in update
SystemError: E:Method http has died unexpectedly!, E:Sub-process http received a segmentation fault.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/snap/snapcraft/876/bin/snapcraft", line 11, in <module>
    load_entry_point('snapcraft==2.36', 'console_scripts', 'snapcraft')()
  File "/snap/snapcraft/876/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 565, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/snap/snapcraft/876/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
    return ep.load()
  File "/snap/snapcraft/876/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2291, in load
    return self.resolve()
  File "/snap/snapcraft/876/usr/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2297, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/cli/__main__.py", line 19, in <module>
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/core.py", line 1043, in invoke
    return Command.invoke(self, ctx)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/cli/__init__.py", line 124, in run
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/core.py", line 553, in forward
    return self.invoke(cmd, **kwargs)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/cli/lifecycle.py", line 140, in snap
    project_options, directory=directory, output=output)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_packer.py", line 45, in snap
    execute('prime', project_options)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 80, in execute
    _Executor(config, project_options).run(step, part_names)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 175, in run
    self._run_step(step, part, part_names)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/lifecycle/_runner.py", line 205, in _run_step
    getattr(part, 'prepare_{}'.format(step))()
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/pluginhandler/__init__.py", line 241, in prepare_pull
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/pluginhandler/__init__.py", line 227, in _fetch_stage_packages
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 294,in get
    with self._apt.archive(self._cache.base_dir) as apt_cache:
  File "/snap/snapcraft/876/usr/lib/python3.6/contextlib.py", line 82, in __enter__
    return next(self.gen)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 142,in archive
    raise e
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 133,in archive
    apt_cache = self._setup_apt(cache_dir)
  File "/snap/snapcraft/876/lib/python3.6/site-packages/snapcraft/internal/repo/_deb.py", line 126,in _setup_apt
  File "/snap/snapcraft/876/lib/python3.6/site-packages/apt/cache.py", line 454, in update
    raise FetchFailedException(e)
apt.cache.FetchFailedException: E:Method http has died unexpectedly!, E:Sub-process http received asegmentation fault.

BTW, before I go any further (since snap might not be ready yet for production), my intention is packaging a set of Python scripts that I want to make sure I can run in 10 years (they don’t need security, etc.). So I wanted not only to have a Virtualenv, but also a fixed version of Python 3 and imagemagick. I have some bash scripts in other project in the same situation too. Do you think snap is a good idea for that? I have already this setup with Docker, but some disadvantages are that 1) docker run needs root, and these are common user scripts that run without a TTY - I don’t want to allow docker in sudoers, 2) containers take a few seconds to startup (I need them fast), 3) Docker’s filesystem is not accessible as Snap’s is, so I can’t use its virtualenv with a code editor.

libc errors are likely due to building on a more-recent Ubuntu than Core, which is currently using 16.04. To ensure compatibility you should have a look at running LXD on your system and using snapcraft cleanbuild to ensure that your snap is built on a clean 16.04 system.

Installing libc as a stage package is almost always the wrong solution because doing that can cause many problems when interacting with files in the core snap, such as using libraries from core.

OK, thanks! I’ll have a look at building it from LXD. But going back to my OP, I think that in many years to come, Python 3.6 will eventually stop building successfully anyway, so maybe I’ll have to figure out a way around Docker’s issues, although I really liked Snap.

You can build Python in a separate part where you specify that your program build after: [python] (python here is a hypothetical name for that part - you can call it anything you feel). e.g.:

    plugin: autotools # I'm guessing this is the right plugin to build it
    source: http://python.example.com/python.tar.gz
      - --prefix=/usr
      - anything-the-python-build-requires
      - anything-the-python-will-require-at-runtime

    after: [python]
    plugin: nil # this will allow you to do any build steps you need
    # I'm unsure whether using the python plugin will pull a different python ver, so I omitted using it
    source: .
    build: |
      ./second build step
    install: |

I was a bit lazy, so instead of setting up LXD, I just run sudo docker run --rm -v $PWD:$PWD -w $PWD snapcore/snapcraft snapcraft instead of snapcraft, and now everything works, since the current container is based on xenial! Thank you!