Issue running first python snap with script location

Hi,

I can create my python based snap but when it tries to run I get the error:

/snap/grabbag/x1/bin/python: can’t open file ‘/var/snap/grabbag/x1/grabbag.py’: [Errno 2] No such file or directory

Here is my snapcraft.yaml. After it builds I can unsquash it and I see the grabbag.py file in its toplevel ‘bin’ folder along with activate files, a few pip files, python shortcuts and a wheel file.

When it installs grabbag.py is in ‘/snap/grabbag/x1/bin’.

‘var/snap/grabbag/x1’ is empty.

What am I doing wrong?

When I set parts/grabbag/plugin to “dump” I’d get the grabbag file in ‘bin’ but then I’d get an exec error or that python couldn’t be found. Setting parts/grabbag/plugin to “python” generates a snap that runs but without the grabbag.py file so to that end I added ‘parts/main/plugin’ and set it to dump.

name: grabbag
base: core22
version: '0.1'
summary: grabbag for the ctrlx core environment
grade: stable
confinement: 
description: |
  #Runs the grabbag python application.

apps:
  grabbag:
    command: bin/python grabbag.py
    plugs:
      - raw-usb
      - removable-media
      - mount-observe
      - mount-control
      - hardware-observe
      - system-observe
    daemon: simple
    restart-condition: always
    passthrough:
      restart-delay: 30s

parts:
  grabbag:
    plugin: python
    source: .
#    source-type: local
#    organize:
#      grabbag.py: bin/grabbag.py
    stage-packages:
      - python3
      - python3-usb
      - python3-pycryptodome
    build-packages:
      - python3-pip
      - python3-pycryptodome
#    build:
#      - pip3 install pycryptodome
  main:
    plugin: dump
    source: .
    organize:
      grabbag.py: bin/grabbag.py

AI just gave me the following from a variation on my search terms:

    override-build: |
      cp my_script.py $SNAPCRAFT_PART_INSTALL/
    prime:
      - my_script.py

I will add this to my parts and update with the results.

Adding these lines didn’t resolve the issue, it only confirmed my earlier findings.

I receive the message: "Detailed information: cannot pack “/root/prime”: snap is unusable due to missing files: path “bin/python” does not exist.

Any ideas? Nothing populates in the “/var/snap/grabbag/x1” folder and “common” is also empty.

command: bin/python grabbag.py

Snap runs command from $SNAP directory, which is /snap/grabbag/current(x1 is the real directory behind) in your environment.

So you get the error, because there is no such file ‘grabbag.py’ in /snap/grabbag/x1 like the error msg said.

There are 2 ways to address this issue i think.

  1. change command to bin/python $SNAP/bin/grabbag.py
  2. change command to bin/grabbag.py directly.

Personally, I prefer the second one.

1 Like

:frowning: hank you for your reply aoyama and the solution.

I’m still learning to navigate the .yaml structure. At some point in the past, this used to work but little changes here and there…

My original issue is that the system was looking for “/var/snap/grabbag/x1/grabbag.py” and not finding it. I wasn’t sure why it was looking there instead of “/snap/grabbag/bin” where I placed the file.

As you pointed out my command was incorrect.

Your number 2 option didn’t work for me (still nothing in /var/snap/grabbag/x1). I’m not sure how to view errors from “snap try” but the host system I will install this on later today has better logging.

Your number 1 option worked and when I went “snap try” I looked in “/var/snap/grabbag/x1” and my application was writing the logs I was expecting and they are still updating. Yay!

Thank you again!

Any immediate pointers on getting pycryptodome to install? What I was doing wasn’t working (once my file started messing up I manually placed the .py file in “/var…” to get it to run and saw errors).

I’m going to start here plugs and hooks and then go from there if it fails.

Maybe because the snap defined as a daemon, I remember that the daemon is started from this path, but I’m not sure.

I just found that the documentation for snap seems to have been completely rebuilt, I searched but couldn’t find any documentation for the details of daemon, which was available before.

I would recommend not using stage-packages to install python libraries.

For example, your snap base is core22, the versions of these stage-packages are the versions when ubuntu22.04 was released, and they will not be updated in the future, which may cause problems.

You should use python-packages, which will be pulled from the pypi repository directly.

see: https://documentation.ubuntu.com/snapcraft/stable/reference/plugins/python_plugin/#python-packages

But using python-packages will bring another problem.

Python-packages will put the python module files in an additional path, which is not in python_library_path by default.

So you need to add an environment variable in snap to add it to the path.

Here is a simple example.

apps:
  anki-desktop:
    command: ./bin/anki
    desktop: meta/gui/anki-desktop.desktop
    environment:
      ANKI_BASE: $SNAP_USER_COMMON
      PYTHONPATH: $SNAP/usr/lib/python3/dist-packages:$SNAP/lib/python3.12/site-packages

Remember to replace python3.12 with the actual path of core22, I remember it was python3.10.

1 Like

This python3 package is not necessary. When your plugin set to python, snap will bring the python interpreter by default, and the version of python3 depends on the base keyword.

1 Like

Thank you again for the pointers, aoyama! I knew almost enough to be dangerous. I have more learning to do now.

Cheers!

Its been quite a few years since I tried to deploy a python app in snapcraft. I gave up due to the difficulties and bizarre lack of clear documentation. There are numerous unresolved forum threads if one cares to search. Flatpack’s support of Python is no better, possibly worse.

Have things changed over the years? Is there a clear guide? Ideally a guide would cover being able to choose the Python version (regardless of core os), incl. deploy Python gui apps too like wxpython etc.