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?