Select to view content in your preferred language

Differences between Notebook execution and Toolbox execution

1966
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
15 Replies
Scott_T_Anderson
Emerging Contributor

So I'm basically dev'ing it in the Notebook and then saving to a .py file and running as a Tool Script.  What parameters would be required?  Typically.  Is this well documented in the Python docs? I can look it up if so.  Again, thanks for your tolerance Duncan.

0 Kudos
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.

 

 

Scott_T_Anderson
Emerging Contributor

I suppose if you're in the same aprx file that you're trying to save from a Tool script, the file may be locked and unable to be saved.  Perhaps I'll try from another aprx and see if this the case.

0 Kudos
Scott_T_Anderson
Emerging Contributor

I guess another question is, is there an elegant way to run a script inside your current session other than a Tool Script or hitting the Run button inside the Notebook environment?

0 Kudos
DuncanHornby
MVP Notable Contributor
Scott_T_Anderson
Emerging Contributor

So I've learned a few things..but not everything..about this particular situation. The functions that were failing I believe are mostly because I was trying to manipulate the aprx I was currently in using a Tool Script, which functions outside of the current environment.  I don't think changes are saved automatically and I don't think they CAN be saved because I'm in the aprx in my own session outside of the Python session.

I've resigned myself to running my scripts via the Notebook interface and using CURRENT as my aprx parameter.  This behaves as expected but is a bit ugly because you're running the script from the Notebook dev environment's Run button.

If anyone else stumbles into this, the difference between how a Notebook script using the CURRENT parameter vs a standalone script trying to access the current aprx file is significant.  I will try to automate the script by using a button instead of a Tool script. 

Thank you Duncan for endeavouring to locate my issues. 

0 Kudos