Automic Workload Automation

 View Only

 Python and Automic

Stefan Rieger's profile image
Stefan Rieger posted Jun 20, 2023 09:44 AM

Hi,

we call the following command from a windows job:

python "path_to_script.py".

If there is an error in the python script, how can we pass the ErrorCode to the job so that it ends accordingly "ENDED_NOT_OK".

Thanks
Stefan

Andrzej Golaszewski's profile image
Andrzej Golaszewski

Hi Stefan Rieger,
at first you have to distinguish between script error and process error. If you have a script error simply uncomment standard error handling lines in windows job and thats all:

python "path_to_script.py"
@set retcode=%errorlevel%
@if NOT %ERRORLEVEL% == 0 goto :retcode


If the returncode of the last command is not zero the job fail with ENDED_NOT_OK

If you get process errors from a python script you have to add some error handling to your script.
A simple python example with errorhandling:

import sys
try:
    x=1/0

except ZeroDivisionError as err:
    print('Handling run-time error:', err)
    sys.exit(1)

After execution the %ERRORLEVEL% is not zero, so the job fail's

Notice that the script statemens must immediately follow the python call, otherwise your error is not passed to the automation engine. If you need to execute command's after your python call, you must handle it manually. 

Hope that helps

Best Regards

Andrzej Golaszewski

Krum Ganev's profile image
Krum Ganev

Hello,

That will mostly depend on what the script is actually returning. Is it numeric code values? Or its printing messages to stderr/stdout.
Generally everything different from 0 is error.
You can control via FILTER or Post-Process Report parsing. (PREP_PROCESS_REPORT).

Another way would be to define Python as external interpretor and execute the code block instead of starting the .py script.

There are few such threads in the community already - LINK1 , LINK2