Select to view content in your preferred language

Differences between Notebook execution and Toolbox execution

2032
15
Jump to solution
03-29-2022 08:24 AM
Scott_T_Anderson
Emerging Contributor

Greetings..I'm running a Python script in the Notebook environment and it works as expected.  If I try to run as a standalone script in a Toolbox, it fails unexpectedly.  Is there a difference, and what are the differences, between the Notebook runtime and the Script/Toolbox runtime?

 

Thanks

0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor

I think the issue is with the save() method on the project. Here is my minimal code:

import arcpy
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Map')[0]
addTab = arcpy.mp.Table(r"C:\Scratch\fGDB_Scratch.gdb\London")
m.addTable(addTab)
# p.save() # This does not work!
p.saveACopy(r"C:\temp\Scott\Scott2.aprx")
arcpy.AddMessage("Saved!")

 I connected the script to a tool script but as there are no input parameters the tool runs as this:

 

DuncanHornby_0-1648655555326.png

 

The Table is added as shown below:

DuncanHornby_1-1648655616031.png

 

Now if I use p.save() instead of p.saveACopy() then the table IS NOT saved to the project. This seems like a bug to me.

 

 

View solution in original post

15 Replies
DuncanHornby
MVP Notable Contributor

What's the error message?

0 Kudos
Scott_T_Anderson
Emerging Contributor

Hey Duncan, thanks for the reply.  I don't get an error message, just inconsistent behaviour.  For instance, if I use the JoinField function in the Notebook environment between a feature and a table, it behaves as expected.  If I run the same script as a Toolbox script, it does not error, completes as expected but fails to make, or maybe save, the join. Is there some automatic functionality in the Notebook environment that saves that join result for me that doesn't exist in the Toolbox environment?

I should add, this is early days for me in Python, so speak to me like I'm a Labrador Retriever...  🙂

0 Kudos
Scott_T_Anderson
Emerging Contributor

Another thought..are the Environment settings different in the Notebook runtime vs the Script runtime?  Things like, AutoCommit, Maintain Fully Qualified Field Names. Do I need to explicitly set these values or are they inherited from the APRX environment at runtime for both the Notebook and Tool Script environments?

0 Kudos
Scott_T_Anderson
Emerging Contributor

Further information.  I'm actually creating a table using the TableToTable Conversion function.  The table gets created, is added to my local geodb, is added to my map and then joined to an existing feature. If i perform these tasks in the Notebook environment, the join is added successfully.  If it do this in the Toolbox environment, it does not create/save the join.  If the table already exists (ie I don't create it in my script..) then it will create and save the join correctly.  So it would appear that something in the TableToTable conversion function is not the same in Notebook vs Toolbox.  Is there some necessity to "save" the table in a special fashion to make it available to the subsequent calls in the Toolbox environment?  It's successfully added to my geodb and map in the Toolbox environment but it fails to execute the join correctly. Fairly annoying...

0 Kudos
Scott_T_Anderson
Emerging Contributor
0 Kudos
DuncanHornby
MVP Notable Contributor

Scott,

Without seeing your code it's difficult to offer up a solution. I have to confess I don't use the notebook environment, and see little use for it.

I would normally develop a script in an IDE such as spyder and either run it there directly or paste it into the python console or wire it up to a script tool interface, which sounds like what you are doing.

I would be surprised if notebook implements environmental settings in a different way to toolbox so my suspicion is that its a flaw in the code and if you shared it with some of your data and I too replicate the problem then that would be good evidence to log a bug with esri.

0 Kudos
Scott_T_Anderson
Emerging Contributor

Thanks Duncan.  I messed around last night doing some stuff and realized that my issue is likely the referencing of items in my current ArcPro session in the Notebook environment vs trying to access these items from a standalone script.  I think this is a fundamental thing I have to understand better.  For instance, in a standalone script, can i get a handle on the aprx, then a map, then add a layer in the map from a standalone script?  Within the Notebook environment it's implicit where these things are going.  From standalone, I'm not sure I'm accessing the map etc correctly to be able to add and modify layers.

0 Kudos
Scott_T_Anderson
Emerging Contributor

Here is a little snippet.  This fails to add the table when run as standalone. I'm not sure that I am addressing the aprx correctly and it's not able to add or save the table addition.  Any insight would be great.

import arcpy

# set geodb to be worked on
arcpy.env.workspace = r"C:\LocalWork\SLN_Replacement\SLN_Replacement.gdb"
#aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx = arcpy.mp.ArcGISProject(r"C:\LocalWork\SLN_Replacement\SLN_Replacement.aprx")
m = aprx.listMaps("Map")[0]

# get handle on table to add
addTab = arcpy.mp.Table(r"C:\LocalWork\SLN_Replacement\SLN_Replacement.gdb\AddressWorking")
# set flag
found = 0

# test for AddressWorking already in the Map and add if needed
tables = m.listTables()
print("Got tables list.")
if tables is not None:
    for table in m.listTables():
        if table.name == "AddressWorking":
            print("Found AddressWorking.")
            arcpy.AddMessage("Found AddressWorking.")
            found = 1
        if found == 0:
            print("Adding AddressWorking.")
            m.addTable(addTab)

# save aprx
aprx.save

# clean up
del aprx, m, tables, found

print("Done.")
arcpy.AddMessage("Done.")

0 Kudos
DuncanHornby
MVP Notable Contributor

When you say run as a "standalone script", how exactly are you doing that? Are you wiring it up to  Tool script interface? If so I don't see you setting any parameters which would be expected as that is what a script tool is. Or are you running it in an IDE or python console outside ArcPro?

0 Kudos