Python mehrfacher subprocess?

1 Antwort

Das klingt nach einem Problem in der Projektstruktur / der Pfadangaben.

Angenommen, es gibt diese drei Skripte:

script1.py

from subprocess import Popen, PIPE

process = Popen(["python", "test/script2.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = process.communicate()
print(output)

test/script2.py

from subprocess import Popen, PIPE

process = Popen(["python", "script3.py"], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = process.communicate()
print(output)

test/script3.py

print("Hello world!")

Dann würde bei expliziter Ausführung von script2.py die script3.py zwar gefunden werden, aber bei Ausführung von script1.py nicht, da der ausführende Interpreter im Verzeichnis von script1.py nachschaut. Der Pfad in script2.py müsste stattdessen test/script3.py lauten.

Den richtigen Verzeichnispfad kannst du auch dynamisch ermitteln.

import os 
script_file_path = os.path.dirname(os.path.realpath(__file__)) + "/script3.py"