6

ArcGIS Pro 2.1 does not allow saving / exporting of a model constructed with ModelBuilder as a Python script.

It seems that the only way we can circumvent this limitation is to click on each individual geoprocessing history item, but, it is not very efficient to do so.

Is there another workaround?

0

3 Answers 3

7

My original answer appears below the double line. I am updating it because when I checked ArcGIS Pro 2.7.2 just now it looks like the functionality that you seek has been returned at that or an earlier version. It can be found on the ModelBuilder tab of the Ribbon under Export | Export To Python File.

enter image description here



The reasons for the removal of this functionality are given at What happened To Export Script Button In ArcGIS Pro? by dflater-esristaff:

The ability to export from ModelBuilder to a Python script was removed from ArcGIS Pro 1.1 because it did not match user expectations to provide a one-to-one export of your model to Python. The scripts generated by exported models were missing nested sub-models, and did not include iterators, feedback loops, and in-line variable substitution. Models with these components would require major changes that most people who use Export are not able to make themselves. There is currently no plan to add this capability back into ArcGIS Pro.

I ceased exporting ModelBuilder models in ArcMap to Python code the day I discovered Copy As Python Snippet because the exported code was far harder to understand and debug due to the way it named variables, lacked understanding of iterators, etc.

I think it is far easier to learn ArcPy in ArcGIS Pro by using Copy Python Command to copy the exact Python syntax needed to run the geoprocessing tool with the same parameter settings previously used, and then paste it into a script that you are writing.

Alternatively, if you want to quickly run your model using ArcPy, dflater-esristaff also describes a workaround of:

Start by building and saving a model, then create a new Python script file (which is just a text file with .py extension), then using a text editor or Python IDE add lines to the new script to import arcpy, import the toolbox containing your model, and finally run the model.

...

import arcpy
arcpy.ImportToolbox(r"c:\pathtotbx\Toolbox.tbx", "mytools")
arcpy.MyModel_mytools(r"c:\modelinputs\Data.gdb\InputFeatures")
2

If you're simply looking to run an ArcGIS Pro model from a raw python script, simply use something like this:

import arcpy
arcpy.ImportToolbox(r"C:/Data/Toolboxname.tbx")
arcpy.Toolboxname.Modelname()

This will run a model ('Modelname') from a specified Toolbox ('Toolboxname')

1

From the February 2019 roadmap, Esri is considering adding this functionality back in.

This is a "mid-term" (possibly by end of 2019, or into 2020) enhancement:

ModelBuilder to Python - Interactive Python snippet generation as you build a model. Can be exported to Python script file (.py).

At the time of posting, ArcGIS Pro 2.3 does not have this functionality. "Mid-term" would probably be Pro 2.5/6/7, possibly Pro 3.x based on current release cycles.

1
  • I just updated my answer to say that this functionality is present in ArcGIS Pro 2.7.2 (and probably slightly earlier versions).
    – PolyGeo
    Commented Apr 30, 2021 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.