Python Pakete erstellen und installieren
Im Paketmanagement wird sich wieder mit Python 3.12 etwas ändern und daher sind auch bei mir Änderungen erforderlich. Die Methode mit Setup.py und distutils wird wie setuptools so wie es aussieht demnächst nicht mehr anwendbar sein. Damit habe ich mir angesehen, wheels aus meinen Programmen zu machen. Die Beschreibungen wie ich das gestalten werde, muss nicht der aktuellste sein, aber für mich denke ich bin ich relativ zukunftssicher. Hier sind Beispiele ausgewählt, die sowohl normalen Python-Code verwendet als auch ein C-Module kompiliert. Zwei weitere Beispiel und den dazugehörigen C-Code und wie ein Pythonmodule-C-Modul erstellt wird, erkläre ich hier. Python C-Module erstellen 1. Filestruktur und benötigte Files. Dazu habe ich mir ein Arbeitsverzeichnis erstellt und die folgende Struktur eingebettet. Dabei sind drei Dateien (setup.py, setup.cfg, pyproject.toml) erforderlich, die unter "/" abgelegt und die dann näher beschreiben werden. Dann wird ein Ordner "/src" und ein Unterordner mit dem Paketnamen erstellt, hier "fib_fibonacci", Die Datei "__init__.py" wird angelegt, bleibt aber leer. Unter "src/Paketname" werden alle Dateien abgelegt, die im Paket installiert werden sollen. Der Ordner wird nicht angelegt, sondern wird beim Paketerstellungsprozess erzeugt und enthält dann die Pakete/Wheels. Auch der Ordner src/*egg-info wird im Paketerstellungsprozess erzeugt. Da es bei diesem Paket um C-Module handelt, die ich kompiliere, liegen hier die zur Laufzeit erstellten Dateien des Kompilierungsprozesses. Das letzte File ist das C-File selbst und wird unter "/" abgelegt. In Gegensatz zu Code der unter Python geschrieben wird, der unter "/src/Paketname" abzulegen ist.
Wenn wir jetzt alles zusammengestellt haben, können wir uns schon mit dem erstellen, der Dateien setup.py, setup.cfg, pyproject.toml beschäftigen. 2. Erstellen der Dateien setup.py, setup.cfg, pyproject.toml Wie ich es verstehe, soll der zukünftige Weg darin bestehen, dass das File "setup.py" lediglich zum Ausführen der in der Datei "setup.cfg" enthaltenen Konfiguration verwendet werden soll. Und wiederum die Datei "pyproject.toml" dazu dienen Teile der in "setup.cfg" enthaltenen Konfiguration zu übernehmen, damit durch das Systemwerkzeuge "pip" das Paketmanagement erfolgt. Da jedoch keine vollständige Ablöse von "setup.py/setup.cfg" durch "pyproject.toml" vorgesehen ist, sind alle drei Dateien erforderlich. Ein Grund liegt an C-Modulen für Python. Diese werden weiterhin unter "setup.py" konfiguriert, und die restliche Konfiguration dazu in "setup.cfg". Lediglich Metadaten, Dependencies und Filestrukturzuordnungen werden derzeit mit "pyproject.toml" umgesetzt. Das kann sich natürlich noch ändern, aber meiner Recherche nach ist, die hier zusammengetragen Konfiguration das aktuellste, das ich finden konnte. 2.1 setup.py
from setuptools import setup, Extension
if __name__ == '__main__':
setup(ext_modules=[Extension('fibonacci', ['Fib.c'])])
#setup() Die letzte Zeile im Script ist mit der davor angeführten auszutauschen, wenn keine C-Module kompiliert werden sollen. Diese Datei ist daher nur ein Placeholder für Aufgaben, die nicht durch die anderen Dateien übernommen werden. Mehr ist hier nicht zu tun. 2.2 setup.cfg
setup.cfg unter root ablegen [metadata]
[metadata]
name = Fips Fibonacci
version = 1.0.0
description = first module Test
keywords = fibonacci
requires-python = "<=3.7"
[options]
package_dir=
=src
packages=find:
[options.packages.find]
where=src Unter Metadaten werden allgemeine Informationen zum Paket übernommen, die aber ebenfalls unter "pyproject.toml" stehen. Jedoch, wenn sie doppelt auftreten, werden diese hier ignoriert. Entsprechend sollte man die Eintragäge einfach entfernen. Beim Paketerstellungsprozess werden entsprechend Warnungen angezeigt, die hier weiterhelfen. Wichtig sind noch die Einträge für "src". So können die erforderlichen Dateien referenziert und gefunden werden. 2.3 pyproject.toml
# pyproject.toml file specified at the root of the directory[build-system]
[build-system]
requires = ["setuptools>=65","wheel"]
# PEP 508 specifications
build-backend = "setuptools.build_meta"
[project]
name = "FipsFibonacci"
version= "1.0.0"
description = "first module Test"
keywords = ["fibonacci"]
requires-python = ">=3.7"
dependencies = ['importlib-metadata; python_version >="3,10"'] Der Inhalt unterscheedet sich nicht sonderlich von "setup.cfg", ist aber für pip wichtig, da es "*.toml"-Dateien lesen kann. Unter "requires" werden die für die Installation notwendigen Pakete mitgegeben und sind unbedingt erforderlich. "name" und "version" wird verwendet, um den Paketnamen unter "/dist" zu erstellen. Wenn die drei Dateien erstellt wurden, und ein entsprechendes Code-File unter "src/Paketname" steht, dann können wir uns die Paketerstellung ansehen. 3. Paket erstellen und Kompilieren. Zunächst installieren wir das erforderliche Paket. Ich verwende "build".
pip install build Nun unter "/" ausführen.
python -m build Als Ergebnis hier der Output.
[fips@jupiter fips_fibonacci]$ python -m build * Creating venv isolated environment... * Installing packages in isolated environment... (setuptools>=65, wheel) * Getting dependencies for sdist... /tmp/build-env-1zwmpd7w/lib64/python3.11/site-packages/setuptools/dist.py:771: UserWarning: Usage of dash-separated 'requires-python' will not be supported in future versions. Please use the underscore name 'requires_python' instead warnings.warn( running egg_info writing src/FipsFibonacci.egg-info/PKG-INFO writing dependency_links to src/FipsFibonacci.egg-info/dependency_links.txt writing requirements to src/FipsFibonacci.egg-info/requires.txt writing top-level names to src/FipsFibonacci.egg-info/top_level.txt reading manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' writing manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' * Building sdist... /tmp/build-env-1zwmpd7w/lib64/python3.11/site-packages/setuptools/dist.py:771: UserWarning: Usage of dash-separated 'requires-python' will not be supported in future versions. Please use the underscore name 'requires_python' instead warnings.warn( running sdist running egg_info writing src/FipsFibonacci.egg-info/PKG-INFO writing dependency_links to src/FipsFibonacci.egg-info/dependency_links.txt writing requirements to src/FipsFibonacci.egg-info/requires.txt writing top-level names to src/FipsFibonacci.egg-info/top_level.txt reading manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' writing manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' warning: sdist: standard file not found: should have one of README, README.rst, README.txt, README.md running check creating FipsFibonacci-1.0.0 creating FipsFibonacci-1.0.0/src creating FipsFibonacci-1.0.0/src/FipsFibonacci.egg-info creating FipsFibonacci-1.0.0/src/fib_fibonacci copying files to FipsFibonacci-1.0.0... copying Fib.c -> FipsFibonacci-1.0.0 copying pyproject.toml -> FipsFibonacci-1.0.0 copying setup.cfg -> FipsFibonacci-1.0.0 copying setup.py -> FipsFibonacci-1.0.0 copying src/FipsFibonacci.egg-info/PKG-INFO -> FipsFibonacci-1.0.0/src/FipsFibonacci.egg-info copying src/FipsFibonacci.egg-info/SOURCES.txt -> FipsFibonacci-1.0.0/src/FipsFibonacci.egg-info copying src/FipsFibonacci.egg-info/dependency_links.txt -> FipsFibonacci-1.0.0/src/FipsFibonacci.egg-info copying src/FipsFibonacci.egg-info/requires.txt -> FipsFibonacci-1.0.0/src/FipsFibonacci.egg-info copying src/FipsFibonacci.egg-info/top_level.txt -> FipsFibonacci-1.0.0/src/FipsFibonacci.egg-info copying src/fib_fibonacci/__init__.py -> FipsFibonacci-1.0.0/src/fib_fibonacci copying src/fib_fibonacci/fibo.py -> FipsFibonacci-1.0.0/src/fib_fibonacci Writing FipsFibonacci-1.0.0/setup.cfg Creating tar archive removing 'FipsFibonacci-1.0.0' (and everything under it) * Building wheel from sdist * Creating venv isolated environment... * Installing packages in isolated environment... (setuptools>=65, wheel) * Getting dependencies for wheel... /tmp/build-env-e5utp7pl/lib64/python3.11/site-packages/setuptools/dist.py:771: UserWarning: Usage of dash-separated 'requires-python' will not be supported in future versions. Please use the underscore name 'requires_python' instead warnings.warn( running egg_info writing src/FipsFibonacci.egg-info/PKG-INFO writing dependency_links to src/FipsFibonacci.egg-info/dependency_links.txt writing requirements to src/FipsFibonacci.egg-info/requires.txt writing top-level names to src/FipsFibonacci.egg-info/top_level.txt reading manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' writing manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' * Installing packages in isolated environment... (wheel) * Building wheel... /tmp/build-env-e5utp7pl/lib64/python3.11/site-packages/setuptools/dist.py:771: UserWarning: Usage of dash-separated 'requires-python' will not be supported in future versions. Please use the underscore name 'requires_python' instead warnings.warn( running bdist_wheel running build running build_py creating build creating build/lib.linux-x86_64-cpython-311 creating build/lib.linux-x86_64-cpython-311/fib_fibonacci copying src/fib_fibonacci/fibo.py -> build/lib.linux-x86_64-cpython-311/fib_fibonacci copying src/fib_fibonacci/__init__.py -> build/lib.linux-x86_64-cpython-311/fib_fibonacci running egg_info writing src/FipsFibonacci.egg-info/PKG-INFO writing dependency_links to src/FipsFibonacci.egg-info/dependency_links.txt writing requirements to src/FipsFibonacci.egg-info/requires.txt writing top-level names to src/FipsFibonacci.egg-info/top_level.txt reading manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' writing manifest file 'src/FipsFibonacci.egg-info/SOURCES.txt' running build_ext building 'fibonacci' extension creating build/temp.linux-x86_64-cpython-311 gcc -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -DNDEBUG -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic - fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack- protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -O2 -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fstack-protector-strong -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/tmp/build-env-e5utp7pl/include -I/usr/include/python3.11 - c Fib.c -o build/temp.linux-x86_64-cpython-311/Fib.o gcc -shared -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -Wl,--build-id=sha1 -g -Wl,-z,relro -Wl,--as-needed -Wl,-z,now -Wl,--build-id=sha1 -g build/temp.linux-x86_64-cpython-311/Fib.o -L/usr/lib64 -o build/lib.linux-x86_64-cpython-311/ fibonacci.cpython-311-x86_64-linux-gnu.so installing to build/bdist.linux-x86_64/wheel running install running install_lib creating build/bdist.linux-x86_64 creating build/bdist.linux-x86_64/wheel copying build/lib.linux-x86_64-cpython-311/fibonacci.cpython-311-x86_64-linux-gnu.so -> build/bdist.linux-x86_64/wheel creating build/bdist.linux-x86_64/wheel/fib_fibonacci copying build/lib.linux-x86_64-cpython-311/fib_fibonacci/__init__.py -> build/bdist.linux-x86_64/wheel/fib_fibonacci copying build/lib.linux-x86_64-cpython-311/fib_fibonacci/fibo.py -> build/bdist.linux-x86_64/wheel/fib_fibonacci running install_egg_info Copying src/FipsFibonacci.egg-info to build/bdist.linux-x86_64/wheel/FipsFibonacci-1.0.0-py3.11.egg-info running install_scripts creating build/bdist.linux-x86_64/wheel/FipsFibonacci-1.0.0.dist-info/WHEEL creating '/home/fips/fips_fibonacci/dist/tmpw74864dg/FipsFibonacci-1.0.0-cp311-cp311-linux_x86_64.whl' and adding 'build/bdist.linux-x86_64/wheel' to it adding 'fibonacci.cpython-311-x86_64-linux-gnu.so' adding 'fib_fibonacci/__init__.py' adding 'fib_fibonacci/fibo.py' adding 'FipsFibonacci-1.0.0.dist-info/METADATA' adding 'FipsFibonacci-1.0.0.dist-info/WHEEL' adding 'FipsFibonacci-1.0.0.dist-info/top_level.txt' adding 'FipsFibonacci-1.0.0.dist-info/RECORD' removing build/bdist.linux-x86_64/wheel Successfully built FipsFibonacci-1.0.0.tar.gz and FipsFibonacci-1.0.0-cp311-cp311-linux_x86_64.whl Im oberen Teil des Commandline-Outputs, rot hinterlegt, befinden sich Warnungen. Diese Warnungen sind Hinweise, und stören das Paket nicht. Hier wird angegeben, dass keine "README.me" angelegt, oder ein Parameter falsch oder deprecated gekennzeichnet wurde. Unter "/dist" findet sich nun das erstellte Paket und kann mit pip installiert werden.
$ pip install dist/FipsFibonacci-1.0.0-cp311-cp311-linux_x86_64.whl 4. Ausführen und Testen. Dazu Python starten und testen
Python 3.11.0rc2 (main, Sep 13 2022, 00:00:00) [GCC 12.2.1 20220819 (Red Hat 12.2.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import fib_fibonacci.fibo >>> import fibonacci >>> print(fibonacci.fibonacci(12)) >>> print(fib_fibonacci.fibo.hallo()) >>> 144 >>> Hallo Weitere Quellen und Suchbegriffe: Python, pip, Paketmanagement, setup, C-Module, Python-Module
geändert, 18. Nov 2022, 23:52 Copyright © 2025 by Philipp
|