I have exported a ModelBuilder into a Python script. I am not able to figure it out at which point of the python script I need to make changes so that I can completely run the python script in Python IDE

Here is the ModelBuilder screen shot

enter image description here

Here is the ModelBuilder script exported to Python Script.

 # Import arcpy module

import arcpy
from arcpy import env
from arcpy.sa import *

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")

# Local variables:
ElevationContourRingSimply = "W:\\Projects\\FacilityProcessingtest\\ShpPython\\HobokenFlood.gdb\\ElevationContourRingSimply"
Value = "20"
fdcb_Value_ = "W:\\Projects\\FacilityProcessingtest\\ShpPython\\HobokenFlood.gdb\\fdcb%Value%"
fdcb_Value___2_ = fdcb_Value_
fdcb_Value___3_ = fdcb_Value___2_
fdcb_Value___4_ = fdcb_Value___3_
fdcb_Value___5_ = fdcb_Value___4_
fdcb_Value___6_ = fdcb_Value___5_

# Process: For
arcpy.IterateCount_mb("1", "20", "1")

# Process: Select
arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= %Value%")

# Process: Add Field
arcpy.AddField_management(fdcb_Value_, "WaterLevel", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

# Process: Calculate Field
arcpy.CalculateField_management(fdcb_Value___2_, "WaterLevel", "%Value%", "VB", "")

# Process: Calculate Field (2)
arcpy.CalculateField_management(fdcb_Value___3_, "GRIDCODE", "%Value% - [GRIDCODE] + 1", "VB", "")

# Process: Alter Field
arcpy.AlterField_management(fdcb_Value___4_, "GRIDCODE", "FloodDepth", "FloodDepth", "", "4", "NON_NULLABLE", "false")

# Process: Delete Field
arcpy.DeleteField_management(fdcb_Value___5_, "FID_FloodAllW")
  • 4
    Have you tried running it and encountered a specific problem that you need to work around?
    – Dan C
    Mar 3, 2016 at 18:04

2 Answers 2


I see several items that will need to be cleaned up. Going from top to bottom...

  1. You need to use the entire toolbox path to import it. A stand-alone script won't know how to find it just by name. Also, open your toolbox properties and make sure you have given it an alias. That way you won't have to type out the whole name every time you use a tool from it.
  2. The For loop will need to be put in python. The iterator is a modelbuilder-only tool. Python can't use it. It looks like you are iterating through a range? If so, that would be "for x in range(1, 20):"
  3. You will need to fix your local variables so python can use them. The reason why you have a bunch of variables that all look alike is because your model is a chain. If your workflow processes are all using the same input, you don't need all the separate variables in python. So pare them down to one.
  4. In your processes, you will need to change the input parameters so python can use them.

For example, instead of "%Value%", you will just put Value, i.e., instead of

 arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= %Value%")

you will put

 arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= " + str(Value))

or if GRIDCODE is a text column

 arcpy.Select_analysis(ElevationContourRingSimply, fdcb_Value_, "GRIDCODE <= \"" + str(Value) + "\"")
  1. If you want to be able to input different parameters, you will need to add user input code at the top of the script. (Param1 = arcpy.GetParameterAsText(0)) If you do that, since you are making a stand-alone script (not running out of an ArcGIS toolbox), you will need to add code to check the user's inputs to make sure they are something that will work. Running scripts out of a toolbox saves you a lot of coding.
  2. You will also want to add some error control. This comes in the form of a try, except, finally statement.

If you know ahead of time that your final tool will be a script tool, it is more efficient to code it as such from the beginning. Making a model first doesn't save you any time or effort. The only reason I ever do that is if the ESRI documentation on a tool is not very clear (at least to me), and I need to see how a working model actually shows up in python.

I hope this is helpful and gets you started.

  • re: the second-last paragraph, a trick is to run only that one tool then look in > Geoprocessing > Results > Copy As Python Snippet to get the Python syntax for a single tool Mar 16, 2016 at 4:32

You're going to have to clean up the output in different areas. The Export to Python script doesn't handle a lot of the newer functionality in Modelbuilder, the FOR iterator for one.

  • You'll need to remove this code and write your own loop logic # Process: For arcpy.IterateCount_mb("1", "20", "1")
  • You can delete this bit arcpy.ImportToolbox("Model Functions")
  • I dont know why, but a lot of circular references were made with the variables pointing at each other. It appears fdcb_Value___6_ actually equals your input dataset.
  • Have a read of this blog for more gotchas to be aware of.

Is there any real reason you've exported your model to script? If you model is working, why not just USE IT, as is? From the last section in the previously mentioned blog, just write 3 lines into a new script to call your model.

import arcpy 
arcpy.ImportToolbox(“c:/mytools/geometrytools.tbx”, “mytools”) 
arcpy.CreateRegularPolygons_mytools(parameter1, parameter2)

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.