Select to view content in your preferred language

Script saved from GP History throws ImportToolbox error

382
2
Jump to solution
05-21-2024 03:21 PM
EvanThoms
Frequent Contributor

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")
arcpy.management.CalculateField(
  in_table="DataSources",
  field="deleteme",
  expression="3",
  expression_type="PYTHON3",
  code_block="",
  field_type="TEXT",
  enforce_domains="NO_ENFORCE_DOMAINS"
)

My question is why is the second line there? When run against the arcgispro-py3 python interpreter, arcpy will be available, thus, so will arcpy.management.CalculateField 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 Solution

Accepted Solutions
HaydenWelch
Frequent 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.

 

HaydenWelch_0-1717613776425.png

HaydenWelch_1-1717613816652.png

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

image.png

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.

View solution in original post

0 Kudos
2 Replies
HaydenWelch
Frequent 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.

 

HaydenWelch_0-1717613776425.png

HaydenWelch_1-1717613816652.png

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

image.png

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
EvanThoms
Frequent Contributor

Ok, that makes sense. Thanks.

I have only ever used the 'Copy Python Command' for pasting into a larger script. The other option came up in a support question at work and I was confused.

0 Kudos