Select to view content in your preferred language

Can’t export a Python script to a model.

1229
2
Jump to solution
03-03-2022 04:20 PM
Labels (1)
Chelsea_Trusdell
New Contributor

In ArcPro users can export a model from ModelBuilder to a Python script, but you can’t export a Python script to a model. why is that ?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

Because a script doesn't have the necessary code to create the model interface, whereas the model contains functioning code which can be extracted from the model


... sort of retired...

View solution in original post

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

Because a script doesn't have the necessary code to create the model interface, whereas the model contains functioning code which can be extracted from the model


... sort of retired...
0 Kudos
Brian_Wilson
Regular Contributor II

Each step in a model can be easily represented as code, so going from model to python is EASY. One box in a model corresponds to a call to an Esri tool in code, along with some set up for parameters.

Theoretically if you did NOTHING to the Python that was exported it would not be impossible to convert back into a model, but what would be the point of that. So, you export and then you start making changes, and soon you have a very readable and elegant Python program that can't be translated back into colored boxes.

About the only time I use Model Builder these days is to generate that initial script, it lets me see how to call the Python API for specific tools. Then it stays in Python. I used to build tools for other users who were not fluent in Python, I would give them a Python toolbox that they could use their models that did specific tasks. Currently I don't need to do that so my tools stay 100% Python.

If you are still using Model Builder I highly recommend this approach: Build Python Toolbox focused on specific tasks then use Model Builder for the high level more abstract view to call the tools.

Don't try to do too much in one tool but it can be a very efficient for example if you are doing processing in Pandas, you make a tool that converts from a feature class to a dataframe at the start, do all the processing in Pandas and then convert back to a feature class at the very end as output. It's (potentially) much faster than the usual Esri model where you have to write the data out to feature classes between each step.

What Esri COULD do, is generate complete Python toolboxes in the export step instead of the current standalone scripts. Then you could modify the internals of the script, save it, and you would have a new completely functional toolbox that could be used in Model Builder. But they don't do that.