ArcMap – How to Release Locks to Temp Files After ArcPy Script Crashes in ArcMap

arcgis-desktoparcmaparcpylock

Sometimes when I'm testing an ArcPy script and it ends with exception, ArcMap is still holding locks to some temp files (I run my scripts through ArcMap).

I cannot run the script any more, because it cannot delete/overwrite the temp files and in the end I have to restart ArcMap to release the locks.

Is there a way to do in a more clever fashion?

Best Answer

I've just come up with the following construction:

def main():
    # my entire code

if __name__ == "__main__":
    try:
        main()
    except Exception, e:
        import gc
        gc.collect()
        import traceback
        arcpy.AddError(traceback.format_exc())

This way I don't loose any messages and so far all locks have been released properly.

Related Question