How can I create a snapcraft.yaml that runs a simple python file?

Hi there!

I am new to snapcraft and I need to create a core22 snap (snapcraft) that runs a simple python file (hello.py). I’ve been messing around with the snapcraft.yaml but I can’t seem to get it working. If anyone has a simple template or knows more about Snapcraft, I’d really appreciate your help.

I’ve tried the following snapcraft.yaml but with no success:

name: hello
base: core22
version: "0.1"
summary: Hello World
description: |
  Python script that prints Hello World

grade: stable
confinement: strict

apps:
  hello:
    command: bin/hello

parts:
  hello:
    plugin: python
    source: .

Thank you so much!

The problem i can see from the given yaml is that there is no provider of python, try using gnome extension, it contains python & related components, to customize further you can set a custom PYTHONPATH

Or, stage needed python & related components , it may involve setting layouts & few env variables, but then there will be no need for extensions.

There’s a basic Python package in every Core based snap by default. You should always have access to a Python environment with working stdlibs even if not explicitly asking for it.

Can you confirm bin/hello actually exists in the final snap? My assumption here is that the Python plugin isn’t actually placing this file in the snap due to the python plugin which assumes the environment is setup with a Python build environment. (setuptools or something similar? Not 100% on the Python plugin itself here). More explicitly, bin/hello isn’t copied from $CRAFT_PART_SRC and $CRAFT_PART_BUILD into $CRAFT_PART_INSTALL where it needs to be; because there isn’t a build system for it yet.

I’d assume the dump plugin might be more ideal here to get a basic example working and you’d be able to transition to python later once we’ve determined what’s fully going on & if it’s actually needed.

Lets see … since your app isnt actually a python module you dont really need to use the python plugin, just copying your hello.py into the right place should be enough … my snapcraft.yaml would look like this:

name: hello
base: core22
version: "0.1"
summary: Hello World
description: |
  Python script that prints Hello World

grade: stable
confinement: strict

apps:
  hello:
    command: bin/hello

parts:
  hello:
    plugin: dump
    source: .
    organize:
      hello.py: bin/hello

To create a snapcraft.yaml for a simple Python file, begin by defining the metadata like the app name, version, and summary. Next, specify the base, which could be core18 or another base suited for your requirements. Under parts, create a part for your Python application, setting the source and plugin. For a simple Python script, the nil plugin suffices. Include the stage-packages to ensure the necessary Python runtime components are available. Specify the command to run your Python file. This could be as simple as python3 your_file.py. Finally, ensure the apps section reflects the app name and command. Once the snapcraft.yaml is defined, run snapcraft to build the snap package. Install and test the snap to ensure the Python script executes correctly within the confined environment of the snap. Adjust the snapcraft.yaml as needed for more complex applications or specific requirements.