# Creating a Flatpak

We can also take care of distributing our PyQt application. Although the [Python Package Index (PyPI)](https://pypi.org/) is probably the standard way to distribute a Python package, [Flatpak](https://flatpak.org/) should be more user friendly for Linux users than using command line tools such as `pip`.

Flatpaks are built locally using `flatpak-builder`. The tool checks out the source code and any of its custom dependencies, then builds it against a runtime (a collection of libraries shared between flatpaks). Since we already took care of the source code, all we need is to write a single file, the flatpak manifest, which describes everything needed to build the package.

Create a new flatpak manifest file `simplemdviewer/org.kde.simplemdviewer.json`:

{% tabs %}
{% tab title="PySide6" %}

```json
{
    "id": "org.kde.simplemdviewer",
    "runtime": "org.kde.Platform",
    "runtime-version": "6.7",
    "sdk": "org.kde.Sdk",
    "base": "io.qt.PySide.BaseApp",
    "base-version": "6.7",
    "command": "simplemdviewer",
    "finish-args": [
    "--share=ipc",
    "--socket=fallback-x11",
    "--socket=wayland",
    "--device=dri",
    "--socket=pulseaudio"
    ],
    "modules": [
        "python3-markdown.json",
        {
            "name": "simplemdviewer",
            "buildsystem" : "simple",
            "build-commands" : [
                "python3 setup.py build",
                "python3 setup.py install --prefix=/app --root=/"
            ],
            "sources": [
                {
                    "type": "archive",
                    "path": "dist/org.kde.simplemdviewer-0.1.tar.gz"
                }
            ]
        }
    ],
    "cleanup-commands": [
        "/app/cleanup-BaseApp.sh"
    ]
}
```

{% endtab %}

{% tab title="PyQt6" %}

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Note

The Flatpak manifest for PySide uses the version 6.7 for the runtime and the base app, as opposed to PyQt which uses the version 6.6. The reason for this is that PySide Flatpak base app is only available from the version 6.7 and upwards.
{% endhint %}

This file reads that we use the `markdown` module and the build info is provided by the `python3-markdown.json` manifest file. We are going to create this manifest automatically using `flatpak-pip-generator`.

Download [flatpak-pip-generator](https://raw.githubusercontent.com/flatpak/flatpak-builder-tools/master/pip/flatpak-pip-generator), and save it into the `simplemdviewer/env/bin/` directory:

```bash
#From within the simplemdviewer/ directory
wget https://raw.githubusercontent.com/flatpak/flatpak-builder-tools/master/pip/flatpak-pip-generator --directory-prefix env/bin
```

The generator has a single dependency to run, `requirements-parser`. After installing it in the virtual environment, the tool can be run:

```bash
python3 -m pip install requirements-parser
python3 env/bin/flatpak-pip-generator markdown
```

You should see a new `python3-markdown.json` inside `simplemdviewer/` now.

We are using the [KDE Runtime](https://docs.flatpak.org/en/latest/available-runtimes.html#kde), which provides Qt dependencies like QtQuick and KDE frameworks like Kirigami, so you will need the Software Development Kit (Sdk) runtime to build the app locally, and the Platform runtime to run it. More than that, the KDE Runtime is based on the general [Freedesktop Runtime](https://docs.flatpak.org/en/latest/available-runtimes.html#freedesktop), which provides Python. You can read more about runtimes in the [Flatpak Runtime Documentation](https://docs.flatpak.org/en/latest/available-runtimes.html).

Install `org.kde.Sdk` and `org.kde.Platform`, version 6.6, from Flathub:

```bash
flatpak install org.kde.Platform/x86_64/6.6 org.kde.Sdk/x86_64/6.6
```

We are using the [PyQt Baseapp](https://github.com/flathub/com.riverbankcomputing.PyQt.BaseApp), which contains an already built and ready-to-use PyQt we can quickly add on top of the KDE Runtime, so we need to install it as well.

For a PyQt Flatpak application, we are using the [PyQt Baseapp](https://github.com/flathub/com.riverbankcomputing.PyQt.BaseApp), which contains an already built and ready-to-use PyQt we can quickly add on top of the KDE Runtime, so we need to install it as well. Alternatively, you can use the [PySide Baseapp](https://github.com/flathub/io.qt.PySide.BaseApp), which provides a similar ready-to-use PySide6 environment.

```bash
flatpak install com.riverbankcomputing.PyQt.BaseApp/x86-64/6.6
```

To attempt a first build of the flatpak, run:

```bash
flatpak-builder --verbose --force-clean flatpak-build-dir org.kde.simplemdviewer.json
```

{% hint style="success" %}
Tip You can add the flag \`--install-deps-from flathub\` to flatpak-builder to make it download the Sdk, Platform and Baseapp for you instead of installing them manually.

If you installed Flathub as a user repository, you will need to add the `--user` flag to install the runtime. Otherwise you might see the error "Flatpak system operation Deploy not allowed for user".
{% endhint %}

Test the flatpak build:

```bash
flatpak-builder --run flatpak-build-dir org.kde.simplemdviewer.json simplemdviewer
```

Build a distributable nightly flatpak bundle:

```bash
flatpak-builder flatpak-build-dir --repo=simplemdviewer-master --force-clean --ccache org.kde.simplemdviewer.json
flatpak build-bundle simplemdviewer-master simplemdviewer.flatpak org.kde.simplemdviewer
```

Now we can either distribute the `simplemdviewer.flatpak` directly to the users, or submit the application to a flatpak repository, like the most popular repository, [Flathub](https://flathub.org/). See the [App Submission Guidelines](https://github.com/flathub/flathub/wiki/App-Submission).

Other improvements you can make to your application:

* Signing the source archive with a [detached signature](https://www.gnupg.org/gph/en/manual/x135.html).
* Providing copyright and licensing information for each file with [REUSE](https://community.kde.org/Guidelines_and_HOWTOs/Licensing).
* Learn more about building flatpak apps with our \[Flatpak Tutorial]\({{< ref "flatpak" >}}) and the [official documentation](https://docs.flatpak.org/en/latest/index.html).
* Consider making it an official \[KDE Application]\({{< ref "add-project" >}}), building \[Flatpak nightlies using KDE infrastructure]\({{< ref "docs/packaging/flatpak/publishing" >}}).
* Follow the [Flathub Quality Guidelines](https://docs.flathub.org/docs/for-app-authors/appdata-guidelines/quality-guidelines) to refine the presentation of your application on Flathub.

Happy hacking.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.novaflowos.com/start/kde-developer-platform/readme/getting-started/python/python-flatpak.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
