Drei Arten dieser Module kann man unterscheiden:
Verwendung von Modules einfach durch Statement:
import module-name
Das funktioniert ohne weiteres
(Für andere, externe Module muss i.a. der PYTHONPATH angepasst werden später)
Beispiel: Python Module für weitere mathematische Funktionen
# mathutil.py: Verschiedene mathematische Hilfsfunktionen import math pi2=math.pi*2. def fak(n): "Berechne Fakultaet" ... def gcd( a, b): "Berechne groessten gemeinsamen Teiler von a und b" ... def fibonacci(n): "Berechne n-te Fibonacci Zahl" ... ...
Verwendung in anderem Python Skript oder interaktiv:
Zuerst Laden des Moduls und dann nutzen der Variablen/Funktionen via
module-name.function(), module-name.variable, z.B.:
import mathutil # import package f20 = mathutil.fak(20) # call function from mathutil umfang = mathutil.pi2 * radius # use variable from mathutil
Alle Verzeichnisse in sys.path:
>>> import sys >>> print(sys.path) ['/home/g/GDuckeck/cip_home/python/test/kurs21', '/software/opt/focal/x86_64/python/3.9-2021.11/lib/python39.zip', '/software/opt/focal/x86_64/python/3.9-2021.11/lib/python3.9', '/software/opt/focal/x86_64/python/3.9-2021.11/lib/python3.9/lib-dynload', ”, '/home/g/GDuckeck/.local/lib/python3.9/site-packages', '/software/opt/focal/x86_64/python/3.9-2021.11/lib/python3.9/site-packages', '/software/opt/focal/x86_64/python/3.9-2021.11/lib/python3.9/site-packages/IPython/extensions', '/home/g/GDuckeck/.ipython']