When I try to run the following simple script from arcmap it fails but when run using right--click debug and then F9 (debug) in python scripter it works fine. I don't understand why???

enter image description here

# Modified by: Georgec Corea (ATGIS) coreagc@gmail.com

# Author:  ESRI
# Date:    July 5, 2010
# Version: ArcGIS 10.0
# Purpose: This script will print one or more map documents to a local printer.
#          The script is intended to run within a script tool.  There are two
#          parameters:
#               1) Select Map Documents to Print,
#               2) Select Output Printer (auto populated using a validation script)
#Notes: The print order of the MXDs is based on how they are entered.  The MXD
#       at the top of the list is first followed by those below it.

import arcpy, string, datetime, shutil, os
import arcpy.mapping as MAP

#Read input parameters from script tool
MXDList = string.split(arcpy.GetParameterAsText(0), ";")
#printer = arcpy.GetParameterAsText(1)

#Loop through each MXD and print
for MXDPath in MXDList:
    arcpy.AddMessage(str(count)+'. Working on '+str(MXDPath)+' @ '+str(datetime.datetime.now())+"\n")
##    try:
    outPDFpath = mxd.filePath[:mxd.filePath.rfind('.')]+'.pdf'
    ddp = mxd.dataDrivenPages
    #MXD = MAP.MapDocument(mxd)
        #arcpy.AddMessage(ddp, MXD)
    if str(os.path.isfile(outPDFpath))=='False':
            outputPDF = arcpy.mapping.PDFDocumentCreate(outPDFpath)
        #arcpy.mapping.ExportToPDF(mxd, outPDFpath)
            arcpy.AddMessage('Printed '+str(outPDFpath)+' @ '+str(datetime.datetime.now())+"\n")
            arcpy.AddMessage('File Exists '+str(outPDFpath)+' skipped pdf creation @ '+str(datetime.datetime.now())+"\n")
##    except:
##        errorm=arcpy.GetMessages()
##        arcpy.AddMessage(' Error creating file...' +str(outPDFpath)+'\n'+str(errorm)+'\n'+' continuing...')

    #MAP.PrintMap(MXD, printer)
    #Remove variable reference to file
del mxd

When run in debug

enter image description here

  • 1
    It would be helpful to include the error that it fails with when run in ArcMap. – Get Spatial Aug 20 '12 at 2:13
  • no error message. It's just the output that I included in the screen grab. – GeorgeC Aug 20 '12 at 2:17
  • Looking at the graphic you included, it is unclear where you are originally trying to run this script. You have ArcCatalog open. Is this where the script fails when run initially? Have you tried to run it in ArcMap? There may be a difference there. In your comment you say no error message. Does it just get caught in a loop and hang, or end with no visible result? Please give some context to the screen grab. In general, more written detail to clarify what we are seeing in the screen grab would help greatly at this point. – Get Spatial Aug 20 '12 at 22:17
  • Sorry...I have tried in arcmap and arcatalog. It just ends (saying tool failed) but no visible result. – GeorgeC Aug 21 '12 at 4:25
  • A trick I use in this case is to either 1) import traceback at the top and wrap the whole script in a try block and them manually use arcpy.AddError(traceback.format_exc()) to get a better stacktrace, or 2) Run it from the command line and get the traceback. You'll have a much better idea of what's going on and we might be able to help more. – nicksan Aug 21 '12 at 21:00

I changed the tool to run only in the foreground and after that it works fine. It would be nice to understand what part of the script can not be run in background mode in a tool.

Thanks Get Spatial and Nicksan for your input.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.