Python apps

Thanks for your feedback, and for taking the trouble to leave your comments. The documentation is generated automatically from the contents of this forum, where anyone can edit, so the docs are the result of a snap-team/community effort.

I can totally see what you mean here. I’ll reword this, and elsewhere where this paragraph is used, to make it more explicit. It should also link to our Base snap documentation, and we really should go through these tutorials to help make them easier to understand and more consistent with some of the snap/snapcraft changes that have been implemented since they were written.

Thanks again.

2 Likes

This doc assumes

  1. The developer knows what setuptools is
  2. The developer has a functioning setuptools configuration

This is not the case if a developer just created a python script and wants to put it into a snap. Maybe we need multiple tutorials for python? One for setuptools and one without setuptools?

source of this complaint

That user wrote this documentation after struggling with this page.

2 Likes

I ran into the same issues as the user you mentioned. The current docs really don’t state that using setuptools is mandatory.

Are there plans of updating these docs or is there some Bug tracker besides this thread to file feedback?

thanks for flagging this - I’ll go through the tutorial and update it.

Just noticed that the snapcraft.yaml file presented here does not exactly match what is grabbed from the git repo for this example. In the repo, confinement is “strict”, and the override-pull scriptlet does a “set-version” as well. Someone should make sure this page is sync’ed with that is in the repo.

Thanks for raising this. I didn’t want to introduce too many concepts into this guide that might get in the way of building the snap. I’ll update the doc to mention this discrepancy.

Hi, I’m having some trouble with snapping my python CLI tool. Reading on the requirements: “It will automatically include these in your snap.” I understand that the python plugin will grab the requirements.txt from my project and automatically include them in the snap. However, if I go and check the archive’s site-packages, they are not there (and an ImportError confirms that).

I had to include them via python-packages for it to work.

image

Is the documentation wrong, or is this a bug?

Thanks for bringing this to our attention! Our instructions above are out of date - you now need to manually add requirements: requirements.txt to point at the location of the requirements file. I’ll give this a quick test and update the document.

1 Like

How would you update python ? :thinking:

Python comes directly from the packages built for the base, so the Python version will be dependent on this. We do hope to provide wider Python version support with a Python-specific extension.

What about using PIP with pyproject files ? or using different tools like pdm or poetry, etc…

Snapcraft currently supports the following:

  • import Python modules with a requirements.txt
  • build a Python project that has a setup.py
  • install packages straight from pip

Although you can usually work around the defaults by overriding the build, such as with the Streamdeck snap (https://github.com/anonymouse64/streamdeck-ui-snap/blob/master/snap/snapcraft.yaml) which does build from pyproject.toml.

1 Like

I can’t get this right from this information:

When trying to “charmcraft pack” (Here is my repo btw)

My setup.py has this and builds fine. It also runs when I “pip install dist/microsample…” - so I think my package works fine.

setup(name=‘microsample’, version=version, py_modules=[‘server’], entry_points={ ‘console_scripts’: [‘start-server=server:main’] } )

My snapcraft.yaml contains this (which is analogue to the description above)

apps: microsample: command: bin/start-server daemon: simple

It doesn’t work with this either by changing the path to start-server:

apps: microsample: command: start-server daemon: simple

How can I debug this?

The official Python example in " Getting started" doesn’t build on a fresh ubuntu 22.04 machine, leading to the error:

Staging yt-dlp 
+ snapcraftctl stage
Priming yt-dlp 
+ snapcraftctl prime
Determining the version from the project repo (version: git).
fatal: not a git repository (or any of the parent directories): .git
Run the same command again with --debug to shell into the environment if you wish to introspect this failure.

The error led me to an old post by popey Git error while building snap which says

version: git really only works if your snapcraft.yaml is in the source tree you’re building…

so why does the official python example use this syntax? The repository https://github.com/yt-dlp/yt-dlp doesn’t contain a snapcraft.yaml. The workaround suggested by popey did work which means arguably the official example should be changed to be:

name: yt-dlp
summary: A fork of youtube-dl with additional features and patches
description: |
      Download and play videos on your local system. Runs from the command
      line and with all the features and patches of youtube-dlc in addition
      to the latest youtube-dl.

#version: git
adopt-info: yt-dlp

grade: stable
confinement: devmode
base: core20
architectures:
  - build-on: [arm64, armhf, amd64]

apps:
  yt-dlp:
    command: bin/yt-dlp
    plugs: [home, network, network-bind, removable-media]

parts:
  yt-dlp:
    plugin: python
    source: https://github.com/yt-dlp/yt-dlp.git
    # https://forum.snapcraft.io/t/git-error-while-building-snap/10130 advice
    override-build: |
      snapcraftctl build
      snapcraftctl set-version $(git describe --tags)

where I replaced version: git with adopt-info: yt-dlp and added the override-build lines.

Some misc thoughts with the documentation page on Python apps:

  • The need for there to be a setup.py for Python apps to build under the snapcraft Python plugin intoduces a steep hurdle to adoption IMO.
  • The inability to use different Python versions, other than what is supplied by default in the base is disappointing and has been a limitation for many years, with developers struggling with workarounds e.g. Build a snap with any version of Python I want

and

  • Building the snap example using snapcraft --use-lxd on a fresh install of ubuntu 22.04 server (then upgraded to add desktop) fails. You have to sudo lxd init --auto to get lxd to work properly - worth mentioning somewhere in the doco, even if its not a specifically python issue.

Summary of my experience:

  1. snapcraft with lxd tooling initially failed, had to google workaround
  2. official yt-dlp Python example failed to build, had to google workaround
  3. can’t begin to package my python project without modifying it and learning about setup.py - it would be nice to be able to create a snap for a normal unpackaged Python program, which 99% of python apps are.
  4. can’t use a version of Python that I want - stuck with what is supplied in corexx
  5. the need to specify additional unknown linux packages in a stage-packages: and build-packages: sections e.g. libssl-dev libjpeg-dev libtiff-dev libgtk2.0-0 etc. etc. is a huge blocker. Even when snapcraft reports library names are needed, those names are not usable and one must apt-file search xxx each one to get a name acceptable to snapcraft.
  6. Various intricate workarounds are needed to get wxpython desktop python snaps to build under core20 and core22 - see https://github.com/Unrud/video-downloader/blob/master/snap/snapcraft.yaml and Python Plugin + Gnome 3 Extension Error
  7. The gnome extension does not play well with the python plugin and snaps cannot get into the snapstore see Core22 gnome extension and broken symlinks without manually editing the snap and removing certain files.

Thanks for your detailed feedback. I think you’re right about a lot of these Getting started pages - I/we should create a task to go through them all. There’s a lot of duplication, some of the details are no longer relevant, and the examples might not always be the best choices. I think it’s important we address these issues as soon as we can. I will go through this one with some urgency though as you raise some good points.

Although not important for your points, the actual example in the Build the snap section clones a fork of the upstream project with a working git version reference (git clone https://github.com/degville/snap-yt-dlp.git), but this isn’t clear in the earlier yaml snippet. This fork also builds for me and contains only two staged packages. If you don’t have LXD installed, snapcraft should prompt for this and install it when it’s first run (I’ve just tried this on a fresh 22.04 and the prompt worked for me).

Thanks again for your feedback. I’ll make sure we update these docs.

this has not been true for a long time, you actually need to give the full path relative to $SNAP … @degville can we fix that ?

1 Like

Yes, of course - thanks for pointing this out. I’ll make sure it’s updated here and in the other guides.

Can this example be updated for core22? At the very least, that would require changing how the version is set (just hardcode it for simplicity), and changing how “architectures” is defined.

This is a good idea, and something we’re aware of. It’s not been done because we should also be using a completely different example snap (for obvious reasons - incidentally, the snap itself is already using core22 if you follow the link). I’ll put some effort into finding or creating one and update this doc. I’ll also make it a priority because Python is important.

Edit: 7th September 2023 This work has now been done. A better example Python project as chosen, now using core22, with updated and more concise yaml.