Building a Python Snap Package


#1

Hello,
I am trying to build snap a package for a python program.

My folder structure looks like this:

  • project-name
    • run.py
    • project-name
      • __init__.py
      • utils.py
    • snapcraft.yaml

Currently I have the following in snapcraft.yaml:

name: project-name
version: 0.1
summary: ...
description: ...

grade: devel
confinement: devmode

parts:
  project-name:
    source: .
    plugin: python
    python-version: python3

apps:
  project-name:
    command: run.py

Now I get the following error:
Failed to generate snap metadata: The specified command 'run.py' defined in the app 'project-name' does not exist or is not executable

I tried to follow the official python tutorial, but they always use remote git repositories and that’s not the case for me.


Python apps
#2

Is run.py in prime? The python plugin leverages setup.py, so it must install it https://python-packaging.readthedocs.io/en/latest/command-line-scripts.html. Alternatively you could use what is explained on Scriptlets to run cp.


#3

Thank you for your answer! I tried to follow the first tip.

I added the following setup.py

from setuptools import setup

setup(
    name="project-name",
    version="0.1.0",
    install_required=[...],
    scripts=["bin/project-name"]
)

Then I added bin/project-name

#!/usr/bin/env python

 import project_name
 project_name.main()

And I added this to snapcraft.yaml

apps:
  project_name-sync:
    command: bin/project-name

But now I get the following error:
FileNotFoundError: [Errno 2] No such file or directory: '.../project-name/bin/project-name

I am sorry for my very basic question!


#4

You might be better off making sure you can, inside a venv and from within the source tree to a pip install .. Following that, make sure your application runs when executed outside that source directory to not accidentally load packages and modules from that tree to ensure everything was installed correctly.

Following that, your snapcraft project should work correctly mostly as is (unless you change command names or package names that is).


#5

I had to delete the folders snapcraft created (parts, prime, snap and stage) and now it finds my file in the bin directory.

Now I have another problem

My program is written in Python 3.6 and uses for example f-strings. Now I get a SyntaxError.
But I can’t tell snapcraft to use python3.6 (python-version: python3.6 results to Failed to load plugin: properties failed to load for project-name: The 'python-version' property does not match the required schema: 'python3.6' is not one of ['python2', 'python3'].
Is it possible to use Python3.6?


#6

I have the same problem. Were you able to find a solution?


#7

For f-strings, you can pip install future-fstrings and use # -*- coding: future_fstrings -*- at the top of the file.

My code depends on other 3.6 features, though, and just installing Python3.6 in the snap doesn’t help, it will default to 3.5.


#8

I got Python 3.6 working based on this comment: Build a snap with any version of Python I want

The sample given there is too complex, though. Here is an easy working example to follow: https://github.com/vaporyproject/vyper/blob/master/snap/snapcraft.yaml