0

I am running a python script from within ArcMap 10.5 and have always noticed that when I need to rerun the script in the same session, I get either an error or python goes to the next line showing just a ">>>". This happens more often on successful runs, but if the script fails because of syntax or data source issue, I can rerun it and it'll be fine. I often have to close out of my MXD and reopen it in order to run the script. It seems when it runs successfully, that there is something left open or the cache needs to be cleared in order for it to run again.

The errors that I get are random and are simply solved by closing the MXD and reopening.

Is there a way that I can clear the cache or force Arc to stop all background processes (if any are running) in Python?

import arcpy
from State_Dictionary import st_list_tup
# DEFINE VARIABLES
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr5 = arcpy.mapping.Layer("Counties")
lyr2 = arcpy.mapping.Layer("Otolaryngologists to Primary Care Physicians")
lyr3 = arcpy.mapping.Layer("Otolaryngologists")
for County in arcpy.mapping.ListLayers(mxd):
 if County.name == "Counties":
 lyr5 = County
for layer in arcpy.mapping.ListLayers(mxd):
 if layer.name == "Otolaryngologists to Primary Care Physicians":
 lyr2 = layer
 if layer.name == "Otolaryngologists":
 lyr3 = layer
st_abbr = "STATE_ABBR = '{}'"
Automate = "Auto_State = '{}'"
count_lyr = "STATE_NAME = '{}'"
for layer in arcpy.mapping.ListLayers(mxd)[0:13]:
lyr2.visible = False
lyr3.visible = False 
for state, fname in st_list_tup:
 layer.visible = True
 layer.definitionQuery = Automate.format(state)
 # CREATE SELECTION LAYER AND WHERE CLAUSE
 arcpy.SelectLayerByAttribute_management(County,"NEW_SELECTION", count_lyr.format(fname))
 # ZOOM TO STATE SELECTION THEN CLEAR SELECTION
 df.zoomToSelectedFeatures()
 # CLEAR SELCTION
 arcpy.SelectLayerByAttribute_management(County, "CLEAR_SELECTION")
 # DEFINITION QUERIES
 lyr5.definitionQuery = count_lyr.format(fname)
 print lyr2.definitionQuery, lyr3.definitionQuery, lyr4.definitionQuery
 # UPDATE MAP ELEMENTS
 for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
 if elm.name == "Title":
 elm.text = "{}".format(layer)
 for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
 if elm.name == "Subtitle":
 elm.text = "{}".format(fname)
 # EXPORT MAP SELECTION TO PNG IMAGE
 arcpy.mapping.ExportToPDF(mxd, r"C:\Data Projects\AMA (43.1GB)\Map Automation\Practice Maps\{}\{} {}.pdf".format(fname, fname, layer))
 # CLEAR DEFINITION QUERIES AND UPDATE MAP VIEW
 arcpy.RefreshActiveView()
# DELETE LOCAL VARIABLES
del mxd, df, lyr3, lyr2, lyr4, lyr5
print "Success!"
GISGe
9,68421 silver badges42 bronze badges
asked Jan 4, 2018 at 13:20
2
  • 1
    there's something being held in memory from the first script. post the code and we can help more. can you post some of the errors you're seeing as well. Commented Jan 4, 2018 at 13:22
  • Not sure why it's not letting me post the entire code in formatted text. Commented Jan 4, 2018 at 13:30

1 Answer 1

1

Try adding this to your script:

from arcpy import env
env.overwriteOutput = True
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
answered Jan 4, 2018 at 20:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.