<?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: Script works as stand alone, fails when ran as script tool with ascii encoding er in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692426#M53676</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hmmmm, I ran as both standalone and script tool (with an unrelated minor change for the image pathname) and both ran fine --- could be because I'm on a different os (32 bit)....or I've made a mistake shuffling the files around.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll keep looking at it.&amp;nbsp; Out of curiosity can you try running it on a different system - maybe hop over to a 10.1 or .2 system?&amp;nbsp; Maybe invoke the 32-bit (if you have the 64-bit installed as well).&amp;nbsp; I'm just trying to understand this better.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 10 Dec 2013 20:19:58 GMT</pubDate>
    <dc:creator>T__WayneWhitley</dc:creator>
    <dc:date>2013-12-10T20:19:58Z</dc:date>
    <item>
      <title>Script works as stand alone, fails when ran as script tool with ascii encoding errors</title>
      <link>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692425#M53675</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am encountering a strange problem with a script I am trying to run. It updates a Site confgiruation .xml file for setting up Silverlight based REST sites. The code below is a function that is imported into another larger script. This code works perfectly when ran as a stand alone script but it fails when ran as a script tool. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the testing code:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
import os, arcpy, codecs, shutil, glob, sys
import xml.etree.ElementTree as ET

def Message(msg):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print str(msg)
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(msg))
&amp;nbsp;&amp;nbsp;&amp;nbsp; 

def AdjustSite(Site_Folder, name, user, image):
&amp;nbsp;&amp;nbsp;&amp;nbsp; _dir = os.path.dirname(sys.argv[0])
&amp;nbsp;&amp;nbsp;&amp;nbsp; site_dir = os.path.join(_dir, 'Sites')
&amp;nbsp;&amp;nbsp;&amp;nbsp; temp_dir =&amp;nbsp; os.path.join(_dir, 'OtherTemplates')
&amp;nbsp;&amp;nbsp;&amp;nbsp; ws = os.path.join(site_dir, Site_Folder)
&amp;nbsp;&amp;nbsp;&amp;nbsp; tags = ['Permissions','PrintTemplates','Security','Viewers','Workflows']
&amp;nbsp;&amp;nbsp;&amp;nbsp; site = os.path.join(ws, 'Site.xml')
&amp;nbsp;&amp;nbsp;&amp;nbsp; shutil.copy(site, site[:-4] + '_Copy.xml')
&amp;nbsp;&amp;nbsp;&amp;nbsp; trees = ET.parse(site)
&amp;nbsp;&amp;nbsp;&amp;nbsp; doc = trees.getroot()
&amp;nbsp;&amp;nbsp;&amp;nbsp; elms = [elm.tag for elm in doc]
&amp;nbsp;&amp;nbsp;&amp;nbsp; with codecs.open(site, 'r', encoding='utf-8') as f:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; orig = f.readlines()[:-1]

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Iterate through tags and add if necessary&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for xtag in tags:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if xtag not in elms:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with codecs.open(os.path.join(temp_dir, '%s.xml'%xtag),'r', encoding='utf-8') as rd:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for l in rd.readlines():
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; orig.append(l.replace('VIEWER_XXX',name).replace('USER_XXX',user).replace('ZZZZ',Site_Folder[:4]))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Message('Added %s tag' %xtag)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Copy Reports
&amp;nbsp;&amp;nbsp;&amp;nbsp; reports = os.path.join(temp_dir, 'Reports')
&amp;nbsp;&amp;nbsp;&amp;nbsp; #--------------------------------------------------------------------------------------------------
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Temporary:&amp;nbsp; Delete Reports if exists&amp;nbsp; &amp;lt;----------- Remove this after testing
&amp;nbsp;&amp;nbsp;&amp;nbsp; rep_fold = os.path.join(ws, 'Reports')
&amp;nbsp;&amp;nbsp;&amp;nbsp; if os.path.exists(rep_fold):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(rep_fold)
&amp;nbsp;&amp;nbsp;&amp;nbsp; #--------------------------------------------------------------------------------------------------
&amp;nbsp;&amp;nbsp;&amp;nbsp; shutil.copytree(reports, os.path.join(ws, 'Reports'))
&amp;nbsp;&amp;nbsp;&amp;nbsp; shutil.copy(image, os.path.join(ws, 'Reports',os.path.basename(image)))
&amp;nbsp;&amp;nbsp;&amp;nbsp; Message('Copied Reports')

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Rename Print templates
&amp;nbsp;&amp;nbsp;&amp;nbsp; for rpx in glob.glob(os.path.join(ws,'Reports','*PrintTemplate*.rpx')):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; os.rename(rpx, rpx.replace('XXXX',Site_Folder[:4]))

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Write out new Site.xml
&amp;nbsp;&amp;nbsp;&amp;nbsp; orig.append('&amp;lt;/Site&amp;gt;')
&amp;nbsp;&amp;nbsp;&amp;nbsp; with codecs.open(site, 'w', encoding='utf-8') as wr:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wr.writelines(orig)
&amp;nbsp;&amp;nbsp;&amp;nbsp; Message('Updated Site.xml for %s' %Site_Folder)
&amp;nbsp;&amp;nbsp;&amp;nbsp; return&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 

if __name__ == '__main__':

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Test Run
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Stand alone params - comment out for script tool run
&amp;nbsp;&amp;nbsp;&amp;nbsp; Site_Folder = 'ConfigTest'
&amp;nbsp;&amp;nbsp;&amp;nbsp; name = 'ConfigViewer'
&amp;nbsp;&amp;nbsp;&amp;nbsp; user = 'calebma'
&amp;nbsp;&amp;nbsp;&amp;nbsp; image =&amp;nbsp; os.path.join(os.getcwd(), r'Logo\Zimmerman.png')

&amp;nbsp;&amp;nbsp;&amp;nbsp; AdjustSite(Site_Folder, name, user, image)

##&amp;nbsp;&amp;nbsp;&amp;nbsp; # Get args
##&amp;nbsp;&amp;nbsp;&amp;nbsp; # script tool params - un-comment this to run as script tool
##&amp;nbsp;&amp;nbsp;&amp;nbsp; argv = tuple(arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount()))
##
##&amp;nbsp;&amp;nbsp;&amp;nbsp; AdjustSite(*argv)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;And here is the error message I am getting from the script tool: &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
&amp;lt;type 'exceptions.UnicodeDecodeError'&amp;gt;: 'ascii' codec can't decode byte 0xae in position 14: ordinal not in range(128)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Essentially, all this is doing is taking an existing xml file and adding some stuff from template xml files (while doing a find/replace) at the end and copying an image to the reports folder. The .rpx files I am looping through are the just xml files. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My guess is there is something going on 'under that hood' where ArcGIS is overwriting the 'utf-8' encoding my script is setting up using the codecs module and encoding it back to ascii (probably to standardize all input params for a script tool?). Has anyone ever encountered anything like this before? I don't know much about unicode/ascii so I don't know what is going on.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have attached the sample data.zip. Everything is relative pathed so to test between stand alone and script tool, the testing variables need to be commented out and un-comment the script tool params. Also, before the script alters the "Site.xml", it creates a backup called "Site_Copy.xml". So after each run, the modified "Site.xml" needs to be deleted and the copy needs to be renamed back to "Site.xml" (these files will be located inside the Sites/ConfigTest folder).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For now, I have a janketty workaround that calls a .bat file to run this script as a python subprocess with the input params so it is ran completely outside of ArcGIS as stand alone. This is obviously not ideal and it seems the issue is occurring from being ran from an ArcGIS Script tool.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:48:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692425#M53675</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-12-12T16:48:35Z</dc:date>
    </item>
    <item>
      <title>Re: Script works as stand alone, fails when ran as script tool with ascii encoding er</title>
      <link>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692426#M53676</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;hmmmm, I ran as both standalone and script tool (with an unrelated minor change for the image pathname) and both ran fine --- could be because I'm on a different os (32 bit)....or I've made a mistake shuffling the files around.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'll keep looking at it.&amp;nbsp; Out of curiosity can you try running it on a different system - maybe hop over to a 10.1 or .2 system?&amp;nbsp; Maybe invoke the 32-bit (if you have the 64-bit installed as well).&amp;nbsp; I'm just trying to understand this better.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Dec 2013 20:19:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692426#M53676</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-12-10T20:19:58Z</dc:date>
    </item>
    <item>
      <title>Re: Script works as stand alone, fails when ran as script tool with ascii encoding er</title>
      <link>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692427#M53677</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Bingo, got the error!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Strange indeed... sorry, Caleb, I simply wasn't paying attention to your script comments!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Let me see here.......try to isolate this further.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Dec 2013 20:36:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692427#M53677</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-12-10T20:36:15Z</dc:date>
    </item>
    <item>
      <title>Re: Script works as stand alone, fails when ran as script tool with ascii encoding er</title>
      <link>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692428#M53678</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here you go Caleb -&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It's a dumb pet trick:&amp;nbsp; I printed your tuple argv and it turns out that is what it's tripping on in passing from the ArcGIS tool interface to python (if I stated that correctly) --- at any rate, the 'encoding' was lost in translation (or not translated, more exactly), so to speak.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I did this, and it 'corrected' itself:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;argv = tuple(str(arcpy.GetParameterAsText(i)) for i in range(arcpy.GetArgumentCount()))&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Dec 2013 20:45:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692428#M53678</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-12-10T20:45:36Z</dc:date>
    </item>
    <item>
      <title>Re: Script works as stand alone, fails when ran as script tool with ascii encoding er</title>
      <link>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692429#M53679</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Here you go Caleb -&lt;BR /&gt;It's a dumb pet trick:&amp;nbsp; I printed your tuple argv and it turns out that is what it's tripping on in passing from the ArcGIS tool interface to python (if I stated that correctly) --- at any rate, the 'encoding' was lost in translation (or not translated, more exactly), so to speak.&lt;BR /&gt;&lt;BR /&gt;I did this, and it 'corrected' itself:&lt;BR /&gt;&lt;BR /&gt;argv = tuple(str(arcpy.GetParameterAsText(i)) for i in range(arcpy.GetArgumentCount()))&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wow.......You have got to be kidding me...Well I feel like an idiot.&amp;nbsp; I never printed out the tuple of args to see what that looked like for the script tool.&amp;nbsp; That's what I get for trying to use shortcuts. Thanks Wayne!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Dec 2013 20:53:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692429#M53679</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2013-12-10T20:53:21Z</dc:date>
    </item>
    <item>
      <title>Re: Script works as stand alone, fails when ran as script tool with ascii encoding er</title>
      <link>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692430#M53680</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Actually, I'm surprised too - I just assumed Unicode was fully supported.&amp;nbsp; ArcGIS does offer at least 'partial' support - I know that because I was quite surprised to find you could use unicode characters for degrees, minutes, seconds in the field calculator....it is still something I haven't wrapped my head around.&amp;nbsp; I guess then the rule of thumb here is to test at every possible point of failure, as silly as it may seem.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm a little surprised ESRI didn't get back to you on this.&amp;nbsp; (And I apologize for not getting back to you sooner myself.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Ah, well, so much to learn and so little time...keep up the good work!&amp;nbsp; By the way, interesting script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Enjoy,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 10 Dec 2013 21:14:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/script-works-as-stand-alone-fails-when-ran-as/m-p/692430#M53680</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-12-10T21:14:41Z</dc:date>
    </item>
  </channel>
</rss>

