Select to view content in your preferred language

Script saved from GP History throws ImportToolbox error

05-21-2024 03:21 PM
Occasional Contributor II

After you run a geoprocessing tool, you can go to the History view, right-click on the entry for the tool you ran, and select 'Save as python script'. 

For example, I saved this as a standalone file:

import arcpy
arcpy.ImportToolbox(r"@\Data Management Tools.tbx")

My question is why is the second line there? When run against the arcgispro-py3 python interpreter, arcpy will be available, thus, so will making ImportToolbox redundant (the help page implies it's just for importing custom toolboxes). Not to mention that the path "@\Data Management Tools.tbx" throws an error when the script is run:

OSError: The toolbox file @\Data Management Tools.tbx was not found.

So, why does 'Save as python script' save a file like that?

0 Kudos
1 Reply
Occasional Contributor

I'm guessing that the Save As Python Script option tries to make sure that you have everything you need to run the script. The @ symbol requires that the script be run through the python interpreter in an active project though (It's referencing a toolbox loaded into memory) so if you run it standalone it will fail because the toolbox isn't loaded.


There's a "Copy Python Command" option that does what you want, just copies the raw function call without trying to make sure it will work in any environment.




If you save a custom python tool run as a script the setup makes more sense:


These paths don't have @ and are absolute. This script save functionality is likely meant to be as generic as possible and they don't check if the toolbox is included with your license and just take the path that's given by the project.

0 Kudos