Python script works in IDLE but not as a tool

1055
7
08-13-2021 06:20 AM
jhakes
by
New Contributor

I am attempting to create a simple script detailed here and here with the goal of batch exporting attachments from an attachments table. 

import arcpy
from arcpy import da
import os

inTable = arcpy.GetParameterAsText(0)
nameField = arcpy.GetParameterAsText(1)
fileLocation = arcpy.GetParameterAsText(2)

desc = arcpy.Describe(inTable)
attTable = desc.file + "__ATTACH"

joinTable = arcpy.management.AddJoin(inTable, 'globalid', attTable, 'rel_globalid')

with da.SearchCursor(joinTable, [attTable + '.data', desc.file + '.' + nameField, desc.file + '.objectid']) as cursor:
    for item in cursor:
        attachment = item[0]
        filenum = str(item[2]) + "_"
        filename = filenum + str(item[1])
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
        del item
        del filenum
        del filename
        del attachment
del cursor

This is the entire script that I have made, designed to find the attachment table based on the user's input table, create a join and then searchcursor through the cursor. 

The script appeared to run successfully without errors, but no files were outputted (simple jpgs). 

In order to test, I simplified the script down to the basic script ESRI provided in the links I inserted above:

import arcpy
from arcpy import da
import os

inTable = r"Y:\GIS_Projects\****\****\****\ProdGIS.sde\****__ATTACH"
fileLocation = r"\\dc01\profiles\****\Desktop\test"
arcpy.AddMessage("Test")

with arcpy.da.SearchCursor(inTable, ['data', 'att_name', 'attachmentid']) as cursor:
    arcpy.AddMessage("Test2")
    for item in cursor:
        arcpy.AddMessage("Test3")
        attachment = item[0]
        filenum = "ATT" + str(item[2]) + "_"
        filename = filenum + str(item[1])
        open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
        del item
        del filenum
        del filename
        del attachment
del cursor

 I added a few arcpy.AddMessage's obviously to see where it is running. It outputs my test 1 and 2 fine, but 3 is never hit. File paths are correct, using ArcGIS Pro 2.8.1, using IDLE 3.7.10, Python 3.7.10.

The biggest issue here is that when I run it as a script via the toolbox, it runs fine with no errors but does not export my attachments. However, when I am editing it via IDLE (right click>edit) in ArcGIS Pro and then run the script from there, it executes perfectly. 

 

What is the disconnect between running it in IDLE and ArcGIS Pro?

0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

arcpy.Exists(inTable)  confirms the existence?


... sort of retired...
0 Kudos
jhakes
by
New Contributor

I checked for existence for inTable, attTable and joinTable. All exist, still no output. Running the script directly through IDLE heeds the desired results, but through ArcGIS I get nothing. Possibly a setting I have improperly configured? I have attached screenshots of my properties and parameters for the script.

parameters.png

general.png

  

0 Kudos
JakeSkinner
Esri Esteemed Contributor

@jhakesI created a similar GP tool.  The code is slightly different, but it should export all attachments from an attachments table.  It's attached below.

0 Kudos
jhakes
by
New Contributor

I appreciate you linking your tool. It's very similar to what I've created, but I gave it a shot and unfortunately it is still not functioning properly. Seems like it's an ArcGIS issue, not a code issue.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Attached is the sample data I used.  Can you test with this and see if you get the same error?

0 Kudos
David_Brooks
MVP Regular Contributor

This might be a stupid question, but is idle from the arcgis install, or did you install python 3.6 from another source? Is it worth removing all your python libraries and reinstalling from myesri?


David
..Maps with no limits..
0 Kudos
DanPatterson
MVP Esteemed Contributor

jakes_tbx.png

compare script notes between your script and Jake's


... sort of retired...
0 Kudos