I have recently created a model tool for a class project and my instructor would like it to be converted to a script tool for practice purposes. I have exported the script from my model and loaded it into a script within a toolbox but there are several things I am unsure about. I have defined the parameters as they were in the model but when I run the script tool it give me multiple errors. I know that the model doesn't pass model only tools to the script (i.e. Select Data) so I'm sure my problem stems from that but I'm not sure what exactly I need to add to fix the problem.

I will attach a copy of my script, tool parameters, tool properties, the original model and the errors I get. I am an amateur with Python so bare with me if I'm asking too much.

NOTE: the '*' represents the actual values but can't be shared

# ---------------------------------------------------------------------------
# CROB1.py
# Created on: 2013-10-08 11:45:20.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: CROB1 <ArcSDE_Connection_File_Location> <Select_Your_AP_County> <Select_Your_AP> <Your_Counties> <Your_AP> 
# Description: 
# This tool creates the layers for management plan map for your AP boundary. The user must still format the Layout View before exporting.
# ---------------------------------------------------------------------------

# Set the necessary product code
import arceditor    
# Import arcpy module
import arcpy

arcpy.env.overwriteOutput = True

# Script arguments
ArcSDE_Connection_File_Location = arcpy.GetParameterAsText(0)
Select_Your_AP_County = arcpy.GetParameterAsText(1)  
Select_Your_AP = arcpy.GetParameterAsText(2)

# Local variables:
FDEP_SDE_Connection_sde = ArcSDE_Connection_File_Location
Counties = FDEP_SDE_Connection_sde
Aquatic_Preserves = FDEP_SDE_Connection_sde

# Process: Create ArcSDE Connection File

arcpy.CreateArcSDEConnectionFile_management(ArcSDE_Connection_File_Location, "FDEP_SDE_Connection", "******", "****", "", "DATABASE_AUTH", "****", "*****", "SAVE_USERNAME", "SDE.DEFAULT", "SAVE_VERSION")

# Process: Select Data
arcpy.SelectData_management(FDEP_SDE_Connection_sde, "BASE.FL_SHORELINE_AREAS")

# Process: Select
arcpy.Select_analysis(Counties, Your_Counties, "\"NAME\" = '%Select Your AP County%' ")

# Process: Select Data (2)
arcpy.SelectData_management(FDEP_SDE_Connection_sde, "DEP.AQUATIC_PRESERVES")

# Process: Select (2)
arcpy.Select_analysis(Aquatic_Preserves, Your_AP, "\"LONG_NAME\" = '%Select Your AP%'")`

enter image description here enter image description here enter image description here enter image description here

  • 1
    When you pasted your code it seems to have lost a lot of the python formatting which makes this code difficult to read, that being said under your local variables section your first line ends with a = which makes no sense in programming, remove it. – Hornbydd Oct 8 '13 at 19:41
  • 4
    Avoid using the export model to script. It's more problematic than its worth. Take a read of this blog by Esri... blogs.esri.com/esri/arcgis/2011/06/24/exportmodeltopy – user22743 Oct 8 '13 at 19:56
  • @Hornbydd Sorry I didn't realize it pasted so funky I can barely read it myself. – landocalrissian Oct 8 '13 at 20:42
  • @user22743 ya I am realizing very quickly that it's almost pointless. – landocalrissian Oct 8 '13 at 20:45
  • 4
    As a developer who has had to fix many, many, many issues in Export Model to Script, I must emphatically discourage its use. A Python script and a GP Model are paradigmatically different and don't translate 1:1 back and forth, so there's loss going to/from Python/ModelBuilder. Also, the code it generates is in no way a decent teaching tool to learn Python. Just don't do it. – Jason Scheirer Oct 8 '13 at 20:50

I agree with Jason's comment about avoiding relying too much (or at all) on the export to Python functionality in ModelBuilder. I also think that if you are brand new to Python and programming in general, it's better to cut your teeth on "pure" Python than Python + ArcGIS. But since your task is to do exactly that, here are my suggestions:

  1. Think about what your model or code is actually trying to accomplish. Write a written description of what the intended purpose of the tool is and place it as a comment at the top of your script, to remind you of your goal.

    Think about how you would do it manually/interactively in ArcGIS. Write this down as a series of steps, or flowchart it if you prefer.

    Try grouping these steps into logical sections, which can later be turned into functions. Think about what inputs and outputs (if any) apply to each of these sections. These will be your function's arguments and return values.

  2. Stub out the basic structure of the script, or use a template.

    • I like to just start off with a few imports I know I'll probably end up using:

      import arcpy, os, sys
    • Stub out the functions I think I'll need:

      def buildWhereClause(table_name, field_name, value):
          # TODO: construct a SQL WHERE clause based on the given parameters
          return "(some hardcoded WHERE clause)"
      def selectFeatures(layer, where_clause=None):
          # TODO: select the features in the given layer and using the optional SQL WHERE clause

      (These are just examples and may be overly simplistic but it hopefully gets the point across.)

    • After that I'll usually have an if __name__ == "__main__" section that either contains or calls a main procedure that is the entry point to the code and starts taking inputs and calling other functions. The purpose of this check is to prevent the code from running if all you want to do is import the module and call its functions independently:

      if __name__ == "__main__":
          # TODO: Get user-entered parameters, hardcode them for now
          input_table = r"C:\test.shp"
          field_name = "NAME"
          field_value = "12345"
          whereclause = buildWhereClause(input_table, field_name, field_value)
          layer = arcpy.MakeFeatureLayer_management(input_table)
          selectFeatures(layer, whereclause)
  3. Test the script, either from the script tool dialog, a Python IDE (if it doesn't need to run inside ArcMap), or by importing the module into the ArcMap Python window and calling specific functions.

  4. Implement the rest of the code, referring to documentation as needed, testing frequently, and iteratively improving the script until it is complete.

See also:

  • 2
    Great answer, and I think the only thing I would add is to be aware that Copy As Python Snippet in the Geoprocessing | Results window will be a far better friend to @ChrisR than Model | Export > To Python Script. – PolyGeo Oct 9 '13 at 7:50
  • @PolyGeo Thanks I have never noticed that option it is much more useful than the model export. – landocalrissian Oct 10 '13 at 14:10
  • @blah238 Thanks for taking the time to give me a really good answer I appreciate it. – landocalrissian Oct 10 '13 at 14:11
  • @RyanDalton thanks for editing my code so it was legible, may I ask how you inserted the code so I can do it in future posts. – landocalrissian Oct 10 '13 at 14:11
  • 1
    @ChrisR, select the code and press Ctrl-K to automatically indent it by 4 spaces to format it correctly. – blah238 Oct 11 '13 at 2:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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