Exit not allowing script to show in Pro

524
6
Jump to solution
07-14-2021 06:14 AM
BrianLeroux
Regular Contributor

I have a python script added to a toolbox in ArcGIS Pro 2.8. I need to terminate early if certain conditions are met but I need the script to complete successfully and not error. I am trying to use sys.exit(0) and it says the script run succeeded in messages. However the History pane is showing it as a Red ! as if it failed which is blocking me from publishing as a web tool. Is there a better way to exit early?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
BrianLeroux
Regular Contributor

I just figured it out. I was able to just use return to move me back to def main.

"""
def main():
    tbx = Toolbox()
    tool = ExposureTool()
    tool.execute(tool.getParameterInfo(),None)

if __name__ == '__main__':
	main()
"""

 

View solution in original post

6 Replies
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Could you provide the code?

 

0 Kudos
BrianLeroux
Regular Contributor

Here is that specific part as the entire code is rather large. What I am trying to do is create a tool that will be published but I do not want people seeing the syntax used for the input. So I need the tool to successfully run by using some placeholder text rather than valid json the tool would need to fully execute.

def execute(self, parameters, messages):
        """The source code of the tool."""
        # setting up needed pieces
        messages = []
        contact_lists = []
        email_list = []

        # setting up logging
        LOG_FILENAME = sys.path[0] + "\\logging_AmicaGP." + dt.now().strftime("%Y%m%d%H%M%S%f") + ".txt"
        logging.basicConfig(filename=LOG_FILENAME, format='%(asctime)s %(levelname)s %(message)s', level=10)
        with open(LOG_FILENAME, 'w'):
            pass
        arcpy.AddMessage("log file: " + LOG_FILENAME)
        logging.debug('This is a LOG file for AMICA GP tool')        
        arcpy.Delete_management("in_memory")
        
        json_in = parameters[0].valueAsText
        if json_in == 'Input Feature':
            logging.debug('Setting up discrete input for publishing purposes')
                       
            sys.exit(0)
0 Kudos
JeffK
by MVP Regular Contributor
MVP Regular Contributor

Not sure this will work, but have you tried wrapping the json_in process into a try/except and in the except block log the debug message and exit?

0 Kudos
JoeBorgione
MVP Esteemed Contributor

Instead of sys.exit() can you  try quit() ?

 

import time 
for x in range(0,10):
    if x == 9:
        print(f'{x} Quit in 5 seconds')
        time.sleep(5)
        quit()
That should just about do it....
0 Kudos
BrianLeroux
Regular Contributor

Quit() is giving me the same result as sys.Exit()

0 Kudos
BrianLeroux
Regular Contributor

I just figured it out. I was able to just use return to move me back to def main.

"""
def main():
    tbx = Toolbox()
    tool = ExposureTool()
    tool.execute(tool.getParameterInfo(),None)

if __name__ == '__main__':
	main()
"""