<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Randomly occuring python errors in script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/randomly-occuring-python-errors-in-script/m-p/272245#M21030</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've got a long script that performs a bunch of different geoprocessing tasks. It creates a File GDB, creates empty feature classes and builds data, often with cursors.&amp;nbsp; 1/2 the time I run the script, it always runs into errors about schema locks or simply fails to run a function that worked previously.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;While my full code is longer and I don't want to share it all, this subsection will throw errors randomly. The latest is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Users\ablakey\Documents\Projects\RapidQuotes\rq.py", line 66, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculatePolygonMainAngle_cartography("waterbodyBound", "angle","GEOGRAPHIC")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\cartography.py", line 350, in CalculatePolygonMainAngle&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcgisscripting.ExecuteError: ERROR 000582: Error occurred during execution.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's as if the code is stumbling over itself when it comes to locks. Is there something I'm missing to create an FC and then have it ready and unlocked when the next function tries to edit it?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
# 0. Delete Previous Data
if arcpy.Exists("c:/Users/ablakey/Documents/Projects/RapidQuotes/geodata/rq.gdb"):&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("c:/Users/ablakey/Documents/Projects/RapidQuotes/geodata/rq.gdb/")
arcpy.CreateFileGDB_management("c:/Users/ablakey/Documents/Projects/RapidQuotes/geodata", "rq.gdb")

# 0.1. Create Empty FCs
arcpy.CreateFeatureclass_management(ws,"pathLines", "POLYLINE", spatial_reference = spatial_reference)


# 1. Buffer Waterbody
arcpy.Buffer_analysis(waterbodyPoly,"waterbodyBuffer",0 - shorelineBuffer)

# 2. Get Bounding Box
arcpy.MinimumBoundingGeometry_management("waterbodyBuffer","waterbodyBound","RECTANGLE_BY_WIDTH")

# 3. Add "angle" Field
arcpy.AddField_management("waterbodyBound","angle","DOUBLE")

# 4. Calculate Angle
arcpy.CalculatePolygonMainAngle_cartography("waterbodyBound", "angle","GEOGRAPHIC")
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 06 Sep 2013 14:34:08 GMT</pubDate>
    <dc:creator>AndrewBlakey1</dc:creator>
    <dc:date>2013-09-06T14:34:08Z</dc:date>
    <item>
      <title>Randomly occuring python errors in script</title>
      <link>https://community.esri.com/t5/python-questions/randomly-occuring-python-errors-in-script/m-p/272245#M21030</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've got a long script that performs a bunch of different geoprocessing tasks. It creates a File GDB, creates empty feature classes and builds data, often with cursors.&amp;nbsp; 1/2 the time I run the script, it always runs into errors about schema locks or simply fails to run a function that worked previously.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;While my full code is longer and I don't want to share it all, this subsection will throw errors randomly. The latest is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Users\ablakey\Documents\Projects\RapidQuotes\rq.py", line 66, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculatePolygonMainAngle_cartography("waterbodyBound", "angle","GEOGRAPHIC")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\cartography.py", line 350, in CalculatePolygonMainAngle&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; raise e&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;arcgisscripting.ExecuteError: ERROR 000582: Error occurred during execution.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's as if the code is stumbling over itself when it comes to locks. Is there something I'm missing to create an FC and then have it ready and unlocked when the next function tries to edit it?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
# 0. Delete Previous Data
if arcpy.Exists("c:/Users/ablakey/Documents/Projects/RapidQuotes/geodata/rq.gdb"):&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("c:/Users/ablakey/Documents/Projects/RapidQuotes/geodata/rq.gdb/")
arcpy.CreateFileGDB_management("c:/Users/ablakey/Documents/Projects/RapidQuotes/geodata", "rq.gdb")

# 0.1. Create Empty FCs
arcpy.CreateFeatureclass_management(ws,"pathLines", "POLYLINE", spatial_reference = spatial_reference)


# 1. Buffer Waterbody
arcpy.Buffer_analysis(waterbodyPoly,"waterbodyBuffer",0 - shorelineBuffer)

# 2. Get Bounding Box
arcpy.MinimumBoundingGeometry_management("waterbodyBuffer","waterbodyBound","RECTANGLE_BY_WIDTH")

# 3. Add "angle" Field
arcpy.AddField_management("waterbodyBound","angle","DOUBLE")

# 4. Calculate Angle
arcpy.CalculatePolygonMainAngle_cartography("waterbodyBound", "angle","GEOGRAPHIC")
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Sep 2013 14:34:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-occuring-python-errors-in-script/m-p/272245#M21030</guid>
      <dc:creator>AndrewBlakey1</dc:creator>
      <dc:date>2013-09-06T14:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: Randomly occuring python errors in script</title>
      <link>https://community.esri.com/t5/python-questions/randomly-occuring-python-errors-in-script/m-p/272246#M21031</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;For a while I had a spate of problems similar to yours, but it seems to have cleared itself up. Not sure if I am just writing less lock-prone code, or if an ArcGIS patch along the way fixed something up. Here are two things which might help you:&lt;/SPAN&gt;&lt;BR /&gt;&lt;UL&gt;&lt;BR /&gt;&lt;LI&gt;Using Exists, Compact, Exists on the GDB in question. This seems to force a refresh and can sometimes clear out the locks. To do it in only one line of code:&lt;/LI&gt;&lt;BR /&gt;[INDENT]&lt;PRE class="lia-code-sample line-numbers language-none"&gt;#input GDB is testWS
[arcpy.Exists(testWS), arcpy.Compact(testWS), arcpy.Exists(testWS)]&lt;/PRE&gt;[/INDENT]&lt;BR /&gt;&lt;LI&gt;I wrote a function that will kill any process, except for the running process, that might have the workspace open. If it is this process that has the workspace open it will repeat the above step indefinitely until the lock disappears. I mostly used this when running scripts that use multiple CPUs at the same time. It can also close ArcMap or Catalog if you accidentally had them open using your workspace. It requires that you import the os library (part of Python), and download, install and import the &lt;A href="https://code.google.com/p/psutil/" rel="nofollow noopener noreferrer" target="_blank"&gt;Psutil library&lt;/A&gt;. It is definitely not fool-proof and can raise exceptions if a process is found that has the workspace open but that process closes its handle before the code gets to doing it. To use, add this to the top of your code:&lt;/LI&gt;&lt;BR /&gt;[INDENT]&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
import psutil

def clearWSLocks(inputWS):
 '''Attempts to clear ArcGIS/Arcpy locks on a workspace.
 Two methods:
&amp;nbsp; 1: if ANOTHER process (i.e. ArcCatalog) has the workspace open, that process is terminated
&amp;nbsp; 2: if THIS process has the workspace open, it attempts to clear locks using arcpy.Exists, arcpy.Compact and arcpy.Exists in sequence

 Required imports: os, psutil
 '''

 # get process ID for this process (treated differently)
 thisPID = os.getpid()

 # normalise path
 _inputWS = os.path.normpath(inputWS)

 # get list of currently running Arc/Python processes
 p_List = []
 ps = psutil.process_iter()
 for p in ps:
&amp;nbsp; if ('Arc' in p.name) or ('python' in p.name):
&amp;nbsp;&amp;nbsp; p_List.append(p.pid)

 # iterate through processes
 for pid in p_List:
&amp;nbsp; try:
&amp;nbsp;&amp;nbsp; p = psutil.Process(pid)
&amp;nbsp; except: # psutil NoSuchProcess...
&amp;nbsp;&amp;nbsp; p = False

&amp;nbsp; # if any have the workspace open
&amp;nbsp; if p and any(_inputWS in pth for pth in [fl.path for fl in p.get_open_files()]):
&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Workspace open: %s' % _inputWS

&amp;nbsp;&amp;nbsp; # terminate if it is another process
&amp;nbsp;&amp;nbsp; if pid != thisPID:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Terminating process: %s' % p.name
&amp;nbsp;&amp;nbsp;&amp;nbsp; p.terminate()
&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! This process has workspace open...'

 # if this process has workspace open, keep trying while it is open...
 while any(_inputWS in pth for pth in [fl.path for fl in psutil.Process(thisPID).get_open_files()]):
&amp;nbsp; print '&amp;nbsp;&amp;nbsp;&amp;nbsp; !!! Trying Exists, Compact, Exists to clear locks: returned %s' % all([arcpy.Exists(_inputWS), arcpy.Compact_management(_inputWS), arcpy.Exists(_inputWS)])

 return True&lt;/PRE&gt;&lt;BR /&gt;then use a function call to try and clear the locks, like so:&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;clearWSLocks(testWS)&lt;/PRE&gt;&lt;BR /&gt;If it often throws exceptions, you could do this (but it may then not clear the locks...):&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; clearWSLocks(testWS)
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; pass&lt;/PRE&gt;[/INDENT]&lt;BR /&gt;&lt;/UL&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me know if either of these help! I haven't used the function for a while, and had to adapt it a bit for posting, so there may be errors in it... You can use trial and error to work out where you need to use these within the code.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 13:17:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/randomly-occuring-python-errors-in-script/m-p/272246#M21031</guid>
      <dc:creator>StacyRendall1</dc:creator>
      <dc:date>2021-12-11T13:17:43Z</dc:date>
    </item>
  </channel>
</rss>

