AnsweredAssumed Answered

Script works as stand alone, fails when ran as script tool with ascii encoding errors

Question asked by Caleb1987 on Dec 10, 2013
Latest reply on Dec 10, 2013 by Wayne_Whitley
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.

Here is the testing code:

import os, arcpy, codecs, shutil, glob, sys
import xml.etree.ElementTree as ET

def Message(msg):
    print str(msg)

def AdjustSite(Site_Folder, name, user, image):
    _dir = os.path.dirname(sys.argv[0])
    site_dir = os.path.join(_dir, 'Sites')
    temp_dir =  os.path.join(_dir, 'OtherTemplates')
    ws = os.path.join(site_dir, Site_Folder)
    tags = ['Permissions','PrintTemplates','Security','Viewers','Workflows']
    site = os.path.join(ws, 'Site.xml')
    shutil.copy(site, site[:-4] + '_Copy.xml')
    trees = ET.parse(site)
    doc = trees.getroot()
    elms = [elm.tag for elm in doc]
    with, 'r', encoding='utf-8') as f:
        orig = f.readlines()[:-1]

    # Iterate through tags and add if necessary 
    for xtag in tags:
        if xtag not in elms:
            with, '%s.xml'%xtag),'r', encoding='utf-8') as rd:
                for l in rd.readlines():
            Message('Added %s tag' %xtag)

    # Copy Reports
    reports = os.path.join(temp_dir, 'Reports')
    # Temporary:  Delete Reports if exists  <----------- Remove this after testing
    rep_fold = os.path.join(ws, 'Reports')
    if os.path.exists(rep_fold):
    shutil.copytree(reports, os.path.join(ws, 'Reports'))
    shutil.copy(image, os.path.join(ws, 'Reports',os.path.basename(image)))
    Message('Copied Reports')

    # Rename Print templates
    for rpx in glob.glob(os.path.join(ws,'Reports','*PrintTemplate*.rpx')):
        os.rename(rpx, rpx.replace('XXXX',Site_Folder[:4]))

    # Write out new Site.xml
    with, 'w', encoding='utf-8') as wr:
    Message('Updated Site.xml for %s' %Site_Folder)

if __name__ == '__main__':

    # Test Run
    # Stand alone params - comment out for script tool run
    Site_Folder = 'ConfigTest'
    name = 'ConfigViewer'
    user = 'calebma'
    image =  os.path.join(os.getcwd(), r'Logo\Zimmerman.png')

    AdjustSite(Site_Folder, name, user, image)

##    # Get args
##    # script tool params - un-comment this to run as script tool
##    argv = tuple(arcpy.GetParameterAsText(i) for i in range(arcpy.GetArgumentCount()))
##    AdjustSite(*argv)

And here is the error message I am getting from the script tool:

<type 'exceptions.UnicodeDecodeError'>: 'ascii' codec can't decode byte 0xae in position 14: ordinal not in range(128)

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.

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.

I have attached the sample 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).

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.