Modelbuilder export to Python, why no import?

3076
5
07-25-2016 08:30 AM
cartographercartographer
New Contributor

ArcGIS 10.4 Desktop, I build myself a model, and it seems if I want to apply it to a different geodatabase, I have to rebuild the model from scratch!   I can't reimport a python script exported from Modelbuilder after changing let's say the input database name across the entire script.   I am using Idle 2.7.10 as an editor.

When I select the input database, if I have many calculations, the following ovals keep their naming from the old model even if the original input database is a different name.  That may confuse the computer if I want to make a different model for a different database.  Do I have to just make the model work in a different MXD just distinct to that database, and separate the geodatabases so the contents with the same name are not overwritten of the individual tables from each other?

0 Kudos
5 Replies
George_Thompson
Esri Frequent Contributor

Exporting a model to Python is not a "replacement" for coding in Python. It was designed as a tool to learn Python.

Here is a good blog and the recent documentation on this:

Considerations when exporting a model to a Python script | ArcGIS Blog

Exporting a model to a Python script—Help | ArcGIS for Desktop

As for the import part, I could see it causing a bunch of issue if you customize the script (add iterators, in-line variable substitution, etc.)

If you create a model in a toolbox, there is a way to call that model via Python. I have seen this be very useful for calling a model that only works in ArcGIS and not as a stand-alone Python script.

ImportToolbox—Help | ArcGIS for Desktop

PythonLearn ArcGIS

--- George T.
DanPatterson_Retired
MVP Emeritus

Anything in your model that does not have a P beside it is hard coded as a local variable.  If you want to be able to change everything, then make all the inputs and outputs parameters.

Export your model to a script and you will see what I mean.  If a variable is not associated with a GetParameterAsText, then you can't change it.  You would be advised to use models as a means to get a shell script, which you then edit and associate the inputs and outputs as variables with tool in a toolbox

PS  I covered this and other options in a series of blog posts... "You are not allowed to use Modelbuilder": When Instructors need to get smarter

cartographercartographer
New Contributor

Thank you both.  Honestly, it would have been nice if ESRI had made a simple background script that reproduced all of the Model's functions that could easily be edited by code or within the model depending on which path one wants to take.  I see though they have made it more complicated than that. 

0 Kudos
XanderBakker
Esri Esteemed Contributor

I would take this opportunity and start to learn python, which is one of the easiest scripting language there is and once you get the hang of it, you can be so much more productive. It is really worth your effort. GeoNet offers many resources to start learning python.

GrantHerbert
Occasional Contributor II

Your model will work against different geodatabases if you make a variable for the database and reference that throughout using %variable% notation. This assumes that input datasets etc will be the same in each case. To avoid overwriting data, use the gdb variable throughout (assuming you want to write to the same gdb as the input).

Once you export the model as a script you can just run the script directly, ie make your changes and go. You may need to add "import arcpy" at the top of the exported script.