I found runpy
standard library most convenient. Why? You have to consider case when error raised in test1.py
script, and with runpy
you are able to handle this in service.py
code. Both traceback text (to write error in log file for future investigation) and error object (to handle error depends on its type): when with subprocess
library I wasn't able to promote error object from test1.py
to service.py
, only traceback output.
Also, comparing to "import test1.py as a module" solution, runpy
is better cause you have no need to wrap code of test1.py
into def main():
function.
Piece of code as example, with traceback
module to catch last error text:
import traceback
import runpy #https://www.tutorialspoint.com/locating-and-executing-python-modules-runpy
from datetime import datetime
try:
runpy.run_path("./E4P_PPP_2.py")
except Exception as e:
print("Error occurred during execution at " + str(datetime.now().date()) + " {}".format(datetime.now().time()))
print(traceback.format_exc())
print(e)