1

I'm using a ArcMap script tool to test if a facility is on a list. If it's not on the list, I want to print a statement and exit the script. The script works just fine as a standalone script but it errors out when I turn it into a script tool.

import arcpy, sys

FacID_raw = arcpy.GetParameterAsText(0)

POT_ID_List = ["AAA", "BBB", "CCC"]

for POT in POT_Search_List:
    POT_ID_List.append(str(POT.getValue("Facility_ID")))

if Fac_ID not in POT_ID_List:
    print Fac_ID + " not in List. Notify team lead."
    sys.exit(0)

The error I get is:

Error Info: <type 'exceptions.SystemExit'>: 0

How do I exit out of a script tool when a condition isn't met?

1
  • 3
    sys.exit() is designed to terminate the process. Since Python is embedded in ArcGIS, this would terminate the ArcGIS Desktop session -- Probably not what your really want to do. Please Edit the question to describe your intended outcome.
    – Vince
    Sep 5, 2019 at 20:18

2 Answers 2

3

You can simply use exit() after your print statement to exit the script.

if Fac_ID not in POT_ID_List:
    print(Fac_ID + " not in List. Notify team lead.")
    exit()

https://docs.python.org/2/library/constants.html#exit

3
  • exit() also throws an error when the script gets imported into the script tool.
    – MSinclair
    Sep 6, 2019 at 17:45
  • 1
    I was able to use exit() in ArcGIS 10.5 in both a script tool and python toolbox without error
    – Pdavis327
    Sep 6, 2019 at 18:32
  • Thanks, @Pdavis327. It must be something with my installation. raise arcpy.ExecuteError threw an error, too.
    – MSinclair
    Sep 6, 2019 at 19:52
2

Using functions allows you to use return to break out of them, and it's just good practice anyway. Since your example is short you could just define a main() function to hold everything:

import arcpy, sys


def main():
    FacID_raw = arcpy.GetParameterAsText(0)

    POT_ID_List = ["AAA", "BBB", "CCC"]

    for POT in POT_Search_List:
        POT_ID_List.append(str(POT.getValue("Facility_ID")))

    if Fac_ID not in POT_ID_List:
        print Fac_ID + " not in List. Notify team lead."
        return

    # do more stuff


main()
1
  • This worked. I'm still peeved that the what's supposed to work isn't, but I'm glad for the work around. Thanks, @mikewatt.
    – MSinclair
    Sep 6, 2019 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.