Pakete für Python-Module und -Anwendungen erstellen¶
Our packaging follows Debian’s Python policy. We will use the python-markdown package as an example, which can be downloaded from PyPI. You can look at its packaging at its Subversion repository.
Es gibt zwei Arten von Python Paketen — Module und Anwendungen
At the time of writing, Ubuntu has two incompatible versions of Python — 2.x and 3.x. /usr/bin/python is a symbolic link to a default Python 2.x version, and /usr/bin/python3 — to a default Python 3.x version. Python modules should be built against all supported Python versions.
If you are going to package a new Python module, you might find the py2dsc tool useful (available in python-stdeb package).
debian/control¶
Python 2.x and 3.x versions of the package should be in separate binary packages. Names should have python{,3}-modulename format (like: python3-dbus.mainloop.qt). Here, we will use python-markdown and python3-markdown for module packages and python-markdown-doc for the documentation package.
Einträge in debian/control die sich ausschließlich auf Python-Pakete beziehen:
Der “section”-Eintrag der Modul-Pakete sollte python und doc für das Dokumentationspaket sein. Für eine Anwendung ist ein Paket mit nur einem Binärpaket ausreichend.
Wir sollten Build-Abhängigkeiten auf python-all (>= 2.6.6-3~) und python3-all (>= 3.1.2-7~) hinzufügen, um sicherzustellen, dass die Python-Hilfswerkzeuge verfügbar sind (der nächste Abschnitt hat hierzu mehr Informationen).
Es wird empfohlen X-Python-Version und X-Python3-Version Einträge hinzuzufügen — siehe Abschnitt “Specifying Supported Versions” der Policy für weitere Details. Zum Beispiel:
X-Python-Version: >= 2.6 X-Python3-Version: >= 3.1
Sollte Dein Paket nur mit Python 2.x oder 3.x funktionieren, die Build-Abhängigkeit auf nur eines der -all Pakete wird ausreichen, ebenso reicht einer der -Version-Einträge.
Modulpakete sollten die Substitutionsvariablen {python:Depends} und respektive {python3:Depends} in ihren Abhängigkeitslisten haben.
debian/rules¶
Die empfohlenen Hilfsprogramme für Pythonmodule sind dh_python2 und dh_python3. Unglücklicherweise baut debhelper noch keine Python 3.x Pakete automatisch (siehe bug 597105 im Debian BTS), also werden wir manuell in den “override” Abschnitten tun müssen (überspringe dies, wenn Dein Paket nicht Python 3.x unterstützt).
Hier ist unsere debian/rules Datei (mit Anmerkungen):
# These commands build the list of supported Python 3 versions
# The last version should be just “python3” so that the scripts
# get a correct shebang.
# Use just “PYTHON3 := $(shell py3versions -r)” if your package
# doesn’t contain scripts
PY3REQUESTED := $(shell py3versions -r)
PY3DEFAULT := $(shell py3versions -d)
PYTHON3 := $(filter-out $(PY3DEFAULT),$(PY3REQUESTED)) python3
%:
# Adding the required helpers
dh $@ --with python2,python3
override_dh_auto_clean:
dh_auto_clean
rm -rf build/
override_dh_auto_build:
# Build for each Python 3 version
set -ex; for python in $(PYTHON3); do \
$$python setup.py build; \
done
dh_auto_build
override_dh_auto_install:
# The same for install; note the --install-layout=deb option
set -ex; for python in $(PYTHON3); do \
$$python setup.py install --install-layout=deb --root=debian/tmp; \
done
dh_auto_install
It is also a good practice to run tests during the build, if they are shipped by upstream. Usually tests can be invoked using setup.py test or setup.py check.
debian/*.install¶
Python 2.x modules are installed into /usr/share/pyshared/ directory, and symbolic links are created in /usr/lib/python2.x/dist-packages/ for every interpreter version, while Python 3.x ones are all installed into /usr/lib/python3/dist-packages/.
If your package is an application and has private Python modules, they should be installed in /usr/share/module, or /usr/lib/module if the modules are architecture-dependent (e.g. extensions) (see “Programs Shipping Private Modules” section of the Policy).
So, our python-markdown.install file will look like this (we’ll also want to install a markdown_py executable):
usr/lib/python2.*/
usr/bin/
und python3-markdown.install wird nur eine Zeile haben:
usr/lib/python3/
Das -doc Paket¶
The tool most commonly used for building Python docs is Sphinx. To add Sphinx documentation to your package (using dh_sphinxdoc helper), you should:
- Füge eine Build-Abhängigkeit auf python-sphinx oder python3-sphinx hinzu (je nachdem, welche Python-Version Du verwendest);
- Hänge sphinxdoc an die Zeile mit dh --with an;
- Lass setup.py build_sphinx in override_dh_auto_build laufen (wird manchmal jedoch nicht benötigt);
- Trag {sphinxdoc:Depends} in die Liste der Abhängigkeiten des -doc Pakets ein;
- Add the path of the built docs directory (usually build/sphinx/html) to your .docs file.
In our case, the docs are automatically built in build/docs/ directory when we run setup.py build, so we can simply put this in the python-markdown-doc.docs file:
build/docs/
Because docs also contain source .txt files, we’ll also tell dh_compress to not compress them — by adding this to debian/rules:
override_dh_compress:
dh_compress -X.txt
Auf Paketierungsfehler prüfen¶
Along with lintian, there is a special tool for checking Python packages — lintian4py. It is available in the lintian4python package. For example, these two commands invoke both versions of lintian and check source and binary packages:
lintian -EI --pedantic *.dsc *.deb
lintian4py -EI --pedantic *.dsc *.deb
Here, -EI option is used to enable experimental and informational tags.
Siehe auch¶
- Die Python policy;
- Python/Packaging article on Debian wiki;
- Python/LibraryStyleGuide and Python/AppStyleGuide articles on Debian wiki;
- Debian python-modules und python-apps Teams.