Skip to main content
Skip to content

Creating a Python package

Understand the requirements to create your own Python package.

Packaging the application

To distribute the application to users we have to package it. We are going to use the setuptools library.

Currently, the project can only be run as a script directly, that is, by running the files directly with python3 /path/to/simplemdviewer_app.py. This is not a convenient way to run a desktop application.

The goal in this page is to make the project available as a console script and as a module:

  • You'll know it is a console script when you are able to run it with simplemdviewer
  • You'll know it is a module when you are able to run it with python3 -m simplemdviewer

If you'd like to learn more about Python packaging, you'll be interested in the Python Packaging User Guide.

General structure

Let's recapitulate what the file structure of the project should be:

simplemdviewer
├── README.md
├── LICENSE.txt
├── MANIFEST.in                # To add our QML file to the Python module
├── pyproject.toml             # The main file to manage the project
├── org.kde.simplemdviewer.desktop
├── org.kde.simplemdviewer.json
├── org.kde.simplemdviewer.svg
├── org.kde.simplemdviewer.metainfo.xml
└── src/
    ├── __init__.py            # To import the src/ directory as a package
    ├── __main__.py            # To signal simplemdviewer_app as the entrypoint
    ├── simplemdviewer_app.py
    ├── md_converter.py
    └── qml/
        └── main.qml

Create a simplemdviewer/pyproject.toml:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "org.kde.simplemdviewer"
version = "0.1"
authors = [{name = "Example Author", email = "example@author.org"}]
maintainers = [{name = "Example Author", email = "example@author.org"}]
description = "A simple markdown viewer"
classifiers = [
    "Development Status :: 5 - Production/Stable",
    "License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)",
    "Intended Audience :: End Users/Desktop",
    "Topic :: Utilities",
    "Programming Language :: Python",
    "Operating System :: POSIX :: Linux",
]
keywords = ["viewer converter markdown"]
urls = {Homepage = "https://mydomain.org/simplemdviewer"}
dependencies = ["markdown"]

[project.readme]
file = "README.md"
content-type = "text/markdown"

[project.scripts]
simplemdviewer = "simplemdviewer.simplemdviewer_app:main"

[tool.setuptools]
packages = ["simplemdviewer"]
package-dir = {simplemdviewer = "src"}
include-package-data = true

[tool.setuptools.data-files]
"share/applications" = ["org.kde.simplemdviewer.desktop"]
"share/icons/hicolor/scalable/apps" = ["org.kde.simplemdviewer.svg"]
"share/metainfo" = ["org.kde.simplemdviewer.metainfo.xml"]

Don't worry about the details for now. We will revisit each part as necessary.

The following four sections of the pyproject.toml are fairly straightforward:

  • [build-system] tells Python to fetch and use setuptools to build the project
  • [project] contains the general metadata for the project
  • [project.readme] specifies a default README file for the project in Markdown
  • [tool.setuptools.data-files] mentions where setuptools should install additional data files that are not typically present in a Python package

App metadata

Let's start with the metadata first so we can get it out of the way, namely the ones listed in the sections [project.readme] and [tool.setuptools.data-files].

Create a simplemdviewer/README.md:

1
2
3
# Simple Markdown Viewer

A simple Markdown viewer created with Kirigami, QML and Python.

Another important piece is the license of our project. Create a simplemdviewer/LICENSE.txt and add the text of the license of our project.

wget https://www.gnu.org/licenses/gpl-3.0.txt --output-document LICENSE.txt

Create a simplemdviewer/MANIFEST.in file with the following contents:

1
include src/qml/*.qml

This file is simply a declaration of additional source code files that should be present in the package when the application runs.

Some last pieces and we are ready to start changing the code. We are going to add:

  1. The Appstream metadata used to show the app in software stores.
  2. A Desktop Entry file to add the application to the application launcher.
  3. An application icon.

Create a new simplemdviewer/org.kde.simplemdviewer.desktop. This file is used to show our Markdown Viewer in application menus/launchers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[Desktop Entry]
Name=Simple Markdown Viewer
Name[ar]=عارض مارك‌داون بسيط
Name[ca]=Visualitzador senzill de Markdown
Name[cs]=Jednoduché prohlížení souborů Markdown
Name[eo]=Simpla Markdown-Vidilo
Name[es]=Sencillo visor de Markdown
Name[fr]=Afficheur simple pour langage « Markdown »
Name[it]=Visore Markdown semplice
Name[ja]=シンプルな Markdown ビューアー
Name[nl]=Eenvoudige Markdown-viewer
Name[pt_BR]=Visualizador de Markdown simples
Name[ro]=Vizualizator simplu de Markdown
Name[ru]=Простая программа просмотра Markdown
Name[sk]=Jednoduchý prehliadač Markdown
Name[sl]=Preprosti ogledovalnik Markdown
Name[sv]=Enkel Markdown-visning
Name[tr]=Basit Markdown Görüntüleyicisi
Name[uk]=Простий переглядач Markdown
Name[zh_TW]=簡單 Markdown 檢視器
GenericName=Markdown Viewer
GenericName[ar]=عارض مارك‌داون
GenericName[ca]=Visualitzador de Markdown
GenericName[cs]=Prohlížeč souborů Markdown
GenericName[eo]=Markdown-Vidilo
GenericName[es]=Visor de Markdown
GenericName[fr]=Afficheur pour langage « Markdown »
GenericName[it]=Visore Markdown
GenericName[ja]=Markdown ビューアー
GenericName[nl]=Markdown-viewer
GenericName[pt_BR]=Visualizador de Markdown
GenericName[ro]=Vizualizator Markdown
GenericName[ru]=Просмотр Markdown
GenericName[sk]=Prehliadač Markdown
GenericName[sl]=Ogledovalnik Markdown
GenericName[sv]=Markdown-visning
GenericName[tr]=Markdown Görüntüleyicisi
GenericName[uk]=Переглядач Markdown
GenericName[zh_TW]=Markdown 檢視器
Comment=A simple Markdown viewer application
Comment[ar]=تطبيق عارض مارك‌داون بسيط