Notes About (Python) Application Packaging

Todo

Connect this to Yocto recipe docs, and vice versa

Installing From Wheel Or Source

Virtual Environment

The FH-ENDLESS/Raspi package has many dependencies, so it is advisable to create a virtual environment for it,

Creating virtual environment
$ python -m venv ~/My-Environments/endless-prototype-test/
Activating virtual environment
$ . ~/My-Environments/endless-prototype-test/bin/activate
(endless-prototype-test) $      # <-- the prompt reflects venv

Installation From Source (Simplest)

(endless-prototype-test) $ python -m pip install /home/jfasch/My-Projects/FH-ENDLESS/Raspi
(endless-prototype-test) $ type run-components
run-components is /home/jfasch/My-Environments/endless-prototype-test/bin/run-components

Installation From Wheel

(See Create Installable Package (A “Wheel”) From Source for how to create a wheel)

(endless-prototype-test) $ python -m pip install ~/My-Projects/FH-ENDLESS/Raspi/dist/endless_prototype-0.1.0-py3-none-any.whl

Create Installable Package (A “Wheel”) From Source

Build Wheel File

$ cd /home/jfasch/My-Projects/FH-ENDLESS/Raspi
$ python3 -m build --wheel
$ ls -l dist/
total 24
-rw-r--r--. 1 jfasch jfasch 20698 Jan 14 10:26 endless_prototype-0.1.0-py3-none-any.whl

What’s In A Wheel

$ unzip -l dist/endless_prototype-0.1.0-py3-none-any.whl
Archive:  dist/endless_prototype-0.1.0-py3-none-any.whl
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  12-16-2024 14:21   endless/__init__.py
        0  12-16-2024 14:21   endless/framework/__init__.py
      580  05-02-2024 08:48   endless/framework/async_util.py
      841  06-07-2024 07:54   endless/framework/can_reader.py
...

“Development Mode”: Editable Install

  • Setup virtual environment for package testing (Virtual Environments (Livehacking Screenplay))

    $ python -m venv ~/My-Environments/endless-prototype-test
    ... roedel ...
    
  • Activate environment

    $ . ~/My-Environments/endless-prototype-test/bin/activate
    (endless-prototype-test) $               # <-- prompt modified
    
  • Install the package into endless-prototype-test, as an editable install. This does not actually create a wheel file, but rather links into the source tree directly. Cool, because now you can continue developing/fixing in the source tree, and at the same time use the package as if you were one of its users. See https://setuptools.pypa.io/en/latest/userguide/development_mode.html.

    (endless-prototype-test) $ python -m pip install --editable ~/My-Projects/FH-ENDLESS/Raspi/
    

Packaging Details

Data Files

End goal is that the endless-prototype package installation brings e.g. sample.conf (to be used by run-conponents) into /etc/endless/. This is not easy!

The preferred Python packaging way nowadays is to write packaging information in pyproject.toml; setup.py and setup.cfg is long deprecated. Platform dependencies (like installing data files into /etc/endless/, for example) are deprecated - “that’s the responsibility of package managers”, they say.

This discussion makes matters clear, especially Michał Górny’s answer: https://discuss.python.org/t/best-practice-for-documentation-its-installation/25159/3

Solution

  • Cram data files into site-packages/endless, where the Python files are.

  • Let the Yocto recipe then sort it all out; for example

    • Move site-packages/endless/sine-plot.conf to /etc/endless/

    • Move site-packages/endless/sine-plot.service to /etc/systemd/system/

Yocto

https://stackoverflow.com/questions/50436413/write-a-recipe-in-yocto-for-a-python-application