AnsweredAssumed Answered

Why does this toolbox script fail the second time? NameError: global name 'arcpy' is not defined

Question asked by flspat on May 21, 2019
Latest reply on May 21, 2019 by Dan_Patterson

Hi All, 

 

Really simple script attached to a toolbox tool:

  • has a wrapper class to convert logging to arcpy.AddMessage. 
  • no input or output parameters
  • has a main function that adds a single message to the window
  • imports arcpy and logging only
  • runs successfully the first time 
  • fails every time after that until the arcmap/arccatalog app is restarted
  • ArcGIS Desktop 10.6.1

 

Zip file attached with script and toolbox. Here's the script:

 

import arcpy 
import logging

class AddMessageHandler(logging.Handler):
    def emit(self, record):
        log_entry = self.format(record)
        arcpy.AddMessage(log_entry)

def main():
    logging.debug("----this is a test----")


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
   
    root_logger = logging.getLogger()
    root_logger.addHandler(AddMessageHandler())

    main()

 

 

First run success: 

 

Subsequent runs fail:

Attachments

Outcomes