<?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 Re: Why would a python script run fine from within ArcMap but not as a geoprocessing service? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606242#M47374</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Dallas - thanks for the reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes the connection exists on the server. For example. there is another feature layer called Properties (that does not have attachments enabled) that exists in the same dataset alongside this 'Jobs' one, and the same code will update a field on the Properties layer no problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the error message can be viewed in the png I attached to the original message (error.png).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;-Paul&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 24 Oct 2014 22:07:49 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2014-10-24T22:07:49Z</dc:date>
    <item>
      <title>Why would a python script run fine from within ArcMap but not as a geoprocessing service?</title>
      <link>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606240#M47372</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I have a python script that opens an edit session on a feature layer and updates a field. It runs perfectly fine from within ArcMap, but after I publish it as a Geoprocessing Service and try to run in from a web app, it throws up an error saying "Objects in this class cannot be updated outside an edit session".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I understand the error, but the script DOES open an edit session.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The feature class has attachments enabled and is not versioned.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Attached is a screenshot of the error from the ArcGIS Server Manager from when I try to run the script from the webpage.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The python script is:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;import arcpy&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;arcpy.env.workspace = "Database Connections\\GISMO.sde"&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;Jobs = "GISMO.DBO.Jobs"&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;JobGuid = '{55729082-574F-46FC-89DE-C9CCC2B4AD96}'&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;edit = arcpy.da.Editor(arcpy.env.workspace)&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;edit.startEditing(False, False)&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;edit.startOperation()&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;whereClause = '"GlobalID" = ' + "'%s'" %JobGuid&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;with arcpy.da.UpdateCursor(Jobs, ["JobStatus"], whereClause) as JobCursor:&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in JobCursor:&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row[0] = 'X'&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; JobCursor.updateRow(row)&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;edit.stopOperation()&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;&lt;EM&gt;edit.stopEditing(True)&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any help here would be much appreciated.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;-Paul&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Oct 2014 22:19:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606240#M47372</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-10-23T22:19:54Z</dc:date>
    </item>
    <item>
      <title>Re: Why would a python script run fine from within ArcMap but not as a geoprocessing service?</title>
      <link>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606241#M47373</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;does &lt;EM style="font-family: arial, helvetica, 'helvetica neue', verdana, sans-serif; color: #e23d39;"&gt;Database Connections\\GISMO.sde&lt;/EM&gt; exist on on the server ? It may be helpful to see the error message also.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Oct 2014 12:50:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606241#M47373</guid>
      <dc:creator>DallasShearer</dc:creator>
      <dc:date>2014-10-24T12:50:52Z</dc:date>
    </item>
    <item>
      <title>Re: Why would a python script run fine from within ArcMap but not as a geoprocessing service?</title>
      <link>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606242#M47374</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Dallas - thanks for the reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes the connection exists on the server. For example. there is another feature layer called Properties (that does not have attachments enabled) that exists in the same dataset alongside this 'Jobs' one, and the same code will update a field on the Properties layer no problem.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the error message can be viewed in the png I attached to the original message (error.png).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;&lt;P&gt;-Paul&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Oct 2014 22:07:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606242#M47374</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2014-10-24T22:07:49Z</dc:date>
    </item>
    <item>
      <title>Re: Why would a python script run fine from within ArcMap but not as a geoprocessing service?</title>
      <link>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606243#M47375</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For anyone reading this, here is the current work-around, thanks to Keven from Esri. It appears that, while the original code I posted works fine within ArcMap, the process of deploying as a service doesn't work - in Kevin's words: "&lt;SPAN lang="EN-US" style="font-size: 11.0pt; font-family: 'Calibri',sans-serif; color: #1f497d;"&gt;Its an issue in the way we parse the script when it gets moved to ArcGIS Server. Some part of the process gets confused, but using the os.path.dirname for some reason keeps it all in line.... &lt;SPAN style="color: #1f497d; font-family: Calibri, sans-serif; font-size: 14.6666669845581px;"&gt;However, moving the &lt;/SPAN&gt;&lt;SPAN lang="EN-US" style="font-size: 11.0pt; font-family: 'Calibri',sans-serif; color: #1f497d;"&gt;arcpy.env.workspace command and changing the way it gets to the path seems to allow it to work correctly. &lt;/SPAN&gt;"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN-US" style="font-size: 11.0pt; font-family: 'Calibri',sans-serif; color: #1f497d;"&gt;&lt;SPAN lang="EN-US" style="font-size: 11.0pt; font-family: 'Calibri',sans-serif; color: #1f497d;"&gt;Here is the code that worked, you'll notice the connection string is slightly different, and the workspace line uses os.path.dirname:&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os, arcpy
newText = arcpy.GetParameterAsText(0).strip()
testHasAttachments = r"Database Connections\TEMPDATA.sde\tempdata.DBO.testhasattachments"
arcpy.env.workspace = os.path.dirname(testHasAttachments)
edit = arcpy.da.Editor(arcpy.env.workspace)
edit.startEditing(False, False)
edit.startOperation()
with arcpy.da.UpdateCursor(testHasAttachments, ["Description", "myIDField"]) as ACursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp; for a in ACursor:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; a[0] = newText
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(a[1]) + " updated successfully.")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ACursor.updateRow(a)
edit.stopOperation()
edit.stopEditing(True)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;-Paul&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 01:58:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-would-a-python-script-run-fine-from-within/m-p/606243#M47375</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T01:58:46Z</dc:date>
    </item>
  </channel>
</rss>

