I want to learn Python and ArcPy by converting ModelBuilder to Python script.

First, I tried to use 'extract by mask' in this way.But it failed.

enter image description here

    # -*- coding: utf-8 -*-
Generated by ArcGIS ModelBuilder on : 2022-07-03 01:56:39
import arcpy
def #  NOT  IMPLEMENTED# Function Body not implemented

def Model1():  # Model

# To allow overwriting outputs change overwriteOutput option to True.
arcpy.env.overwriteOutput = False

# Check out any necessary licenses.

tif = "D:\\04.crash\\tif"
sd_shp = "D:\\04.crash\\shp\\sd.shp"

for fa_tif, Name in #  NOT  IMPLEMENTED(tif, "", "", "NOT_RECURSIVE"):

    # Process: Extract by Mask (Extract by Mask) (sa)
    Name = "fa"
    new_Name_ = fr"D:\04.crash\newtif\new{Name}"
    Extract_by_Mask = new_Name_
    new_Name_ = arcpy.sa.ExtractByMask(in_raster=fa_tif, in_mask_data=sd_shp)

if __name__ == '__main__':
    # Global Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"D:\test\MyProject.gdb", workspace=r"D:\test\MyProject.gdb"):

enter image description here

  File "<string>", line 6
    def #  NOT  IMPLEMENTED# Function Body not implemented
SyntaxError: invalid syntax

It seems that the line of def # NOT IMPLEMENTED# Function Body not implemented is wrong, but I don't konw how to fix it.

However, click 'Run' of ModelBuilder is OK in ArcGIS Pro.

I tried to write some code after reading the code samples of esri.But it still failed. And the error suggests "invalid pointer"

import arcpy
from arcpy.sa import *

# Set the current workspace
arcpy.env.workspace = "D:/04.crash/tif"

# Get and print a list of tifs from the workspace
rasters = arcpy.ListRasters("*", "tif")

# Get the shp
shp = r"D:/04.crash/shp/sd.shp"

# Loop
for raster in rasters:
    outExtractByMask = ExtractByMask(raster, shp)

enter image description here

  • 1
    Start from scratch and: ListRasters, for each raster Extract by mask. Look at the code samples. Probably easier than fixing the exported code
    – BERA
    Jul 2, 2022 at 18:44
  • 1
    Thank for your advice! But why converting ModelBuilder to Python script reported errors? I want to know why.
    – jackywang
    Jul 2, 2022 at 18:50
  • Iterators don't export into python. So any looping logic and inline substitution won't work.
    – Hornbydd
    Jul 3, 2022 at 22:25

1 Answer 1


Using ModelBuilder as a way to "learn" Python is sometimes a good idea, other times a bad idea. While it's great to get you started, there is only so much it can cover. You've discovered that exporting a model to a Python script does not re-create the iterators. See this section of help.

Model tools Iterators, Utilities, or Logical tool will not be exported to Python. You will need to add equivalent Python functionality that these tools provide. For example, you will need to use if/else logic to perform conditional branching in your script.

The code you provide (which is erroring): for fa_tif, Name in # NOT IMPLEMENTED(tif, "", "", "NOT_RECURSIVE"): is simply starter code that you need to finish. It looks like in the edit you made to your post, based on comments, you discovered that arcpy.ListRasters would work as the generator for your script.

Per your second question, and why is it erroring? It looks like it should work, my only guess is that you haven't updated your output name and the code is continually trying to overwrite the output outExtractByMask.save("D:/04.crash/newtif")

You'd need to do something like the following to update the name of the new raster:

import os
for raster in rasters:
    outputName = os.path.basename(raster)
    outExtractByMask = ExtractByMask(raster, shp)

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.