Modul im Package auch für sich ausführen: Importproblem
Verfasst: Sonntag 4. Juni 2017, 16:51
Gegeben folgende Verzeichnisstruktur:
[codebox=text file=Unbenannt.txt]paket_versuche
haupt.py
pypack
__init__.py (leer)
mod_a.py
mod_b.py
[/code]
mod_a.py import nichts. mod_b.py importiert mod_a. haupt.py importiert mod_a und mod_b. Jedes Modul hat eine Funktion, die etwas tut und dazu eine kleine main-Funktion zum Testen und den entsprechenden Abschnitt
Das Problem: ich bekomme den Import von mod_a in mod_b nicht so hin, dass ich sowohl haupt.py als auch mod_b.py ausführen könnte.
Import in haupt.py:
Importversuche in mod_b.py:
Variante a: Ausführen von haupt.py klappt ohne Fehler; Ausführen von mod_b.py ergibt:
Variante b: Jetzt lässt sich mod_b.py ausführen, aber haupt.py nicht:
Die Dokumentation hat mir nicht weitergeholfen, in diesem Forum habe ich auch nichts Passendes gefunden. Geht das nicht, was ich möchte? Dann müsste ich wohl Tests der Moduln in pypack auf die gleiche Ebene verlagern, auf der das Hauptprogramm schon liegt?
Das Ganze mit Python 3.6.1 unter Windows 10, aber das spielt wahrscheinlich hier keine Rolle.
Für den Fall, dass jemand sich das näher ansehen möchte, hier der komplette Code:
[codebox=text file=Unbenannt.txt]paket_versuche
haupt.py
pypack
__init__.py (leer)
mod_a.py
mod_b.py
[/code]
mod_a.py import nichts. mod_b.py importiert mod_a. haupt.py importiert mod_a und mod_b. Jedes Modul hat eine Funktion, die etwas tut und dazu eine kleine main-Funktion zum Testen und den entsprechenden Abschnitt
Code: Alles auswählen
if __name__ == "__main__":
main()
Das Problem: ich bekomme den Import von mod_a in mod_b nicht so hin, dass ich sowohl haupt.py als auch mod_b.py ausführen könnte.
Import in haupt.py:
Code: Alles auswählen
import pypack.mod_a
import pypack.mod_b
Code: Alles auswählen
import pypack.mod_a as ma # Variante a
import mod_a as ma # Variante b
Code: Alles auswählen
Traceback (most recent call last):
File "D:\Sibylle\Src\Python\paket_versuche\pypack\mod_b.py", line 5, in <module>
import pypack.mod_a as ma
ModuleNotFoundError: No module named 'pypack'
Code: Alles auswählen
Traceback (most recent call last):
File "D:/Sibylle/Src/Python/paket_versuche/haupt.py", line 8, in <module>
import pypack.mod_b
File "D:/Sibylle/Src/Python/paket_versuche\pypack\mod_b.py", line 6, in <module>
import mod_a as ma
ModuleNotFoundError: No module named 'mod_a'
Das Ganze mit Python 3.6.1 unter Windows 10, aber das spielt wahrscheinlich hier keine Rolle.
Für den Fall, dass jemand sich das näher ansehen möchte, hier der komplette Code:
Code: Alles auswählen
# mod_a.py
# In pypack
def test(msg):
print("Hier mod_a.test mit Text '{}'".format(msg))
def main():
test("Meldung")
if __name__ == "__main__":
main()
Code: Alles auswählen
# mod_b.py
# In pypack
#import pypack.mod_a as ma # Variante a
import mod_a as ma # Variante b
def test(msg):
print("Hier mod_b.test mit Text '{}'".format(msg))
ma.test("mod_b ruft mod_a mit <{}>".format(msg))
def main():
test("Meldung")
if __name__ == "__main__":
main()
Code: Alles auswählen
#!/usr/bin/env python3
# haupt.py
# Hauptprogramm für Paketversuche
import pypack.mod_a
import pypack.mod_b
def test(msg):
print("Hier haupt.test mit Text '{}'".format(msg))
pypack.mod_a.test("haupt ruft mod_a mit <<{}>>".format(msg))
pypack.mod_b.test("haupt ruft mod_b mit <<{}>>".format(msg))
def main():
test("Meldung")
if __name__ == "__main__":
main()