Is there a way to export a model to a Python script and avoid the local variable paths from being hard-coded? In the sample code below, you can see the export took the effort to make the beginning parameters into variables but the rest of the script still has the same path hard-coded. What's up with that?
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# deletethis.py
# Created on: 2014-08-26 11:36:22.00000
# (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Local variables:
TEMP_Point = "N:\\TechTemp\\BlakeT\\Work\\TEMP.gdb\\TEMP_Point"
TEMP_gdb = "N:\\TechTemp\\BlakeT\\Work\\TEMP.gdb"
# Process: Table to Table
arcpy.TableToTable_conversion(TEMP_Point, TEMP_gdb, "TEMP_Pont_Output", "", "Note1 \"Note1\" true true false 50 Text 0 0 ,First,#,N:\\TechTemp\\BlakeT\\Work\\TEMP.gdb\\TEMP_Point,Note1,-1,-1;Note2 \"Note2\" true true false 50 Text 0 0 ,First,#,N:\\TechTemp\\BlakeT\\Work\\TEMP.gdb\\TEMP_Point,Note2,-1,-1", "")
Solved! Go to Solution.
Hi Blake,
The only place it hard-coded any values was during the optional field mapping, which unless you are using you could remove all the optional parameters out, and only work with the 3 required parameters. I don't believe there is anyway to stop this from happening, but if you are not using the optional parameters, simply removing them after exporting the script.
Hi Blake,
The only place it hard-coded any values was during the optional field mapping, which unless you are using you could remove all the optional parameters out, and only work with the 3 required parameters. I don't believe there is anyway to stop this from happening, but if you are not using the optional parameters, simply removing them after exporting the script.
Ah, you are correct. I removed all the field mapping junk from the Python script and it went just fine. I've noticed this same issue on some other tools as well (not just Table to Table). Is this just one of those things everyone knows about and just works around by manually editing the Python script code?
I would always check scripts exported from modelbuilder or from a geoprocessing result, they are usually a good starter for you to work with, but can usually be simplified a good deal. I am curious which other tools you have seen similar results, usually if the file path or file name is a parameter of some sort it it gets converted to a variable, otherwise I suppose it would hard code it.
I always just find it convenient that you can export a gp result or model to script, its a simple way to get a script started that you will need to build into something more complicated.
I'm on ArcGIS 10.2.2. I've also seen this in TableToGeodatabase (multiple), FeatureClassToFeatureClass, Append, and Merge. There's probably more but those are the ones I use most frequently.
Thanks for your help Ian!
All of those but Table to Geodatabase use field mapping, so thats not so surprising.
Yeah, the table to geodatabase was extra weird because it made local variables but still hard-coded them in the parameters of the script. It didn't even call the variables!
EDIT: I see it did use the variable for the output parameter but the input tables parameter has them all hard-coded even though it made variables.
# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# TableToGeodatabaseMultiple.py
# Created on: 2014-08-26 12:40:36.00000
# (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy
# Local variables:
PD_GroupHome_Status = "N:\\TechTemp\\BlakeT\\Work\\GroupHome_Helpdesk43929\\GroupHome.gdb\\PD_GroupHome_Status"
PD_GroupHome_Type = "N:\\TechTemp\\BlakeT\\Work\\GroupHome_Helpdesk43929\\GroupHome.gdb\\PD_GroupHome_Type"
TEMP_gdb = "N:\\TechTemp\\BlakeT\\Work\\TEMP.gdb"
# Process: Table to Geodatabase (multiple)
arcpy.TableToGeodatabase_conversion("N:\\TechTemp\\BlakeT\\Work\\GroupHome_Helpdesk43929\\GroupHome.gdb\\PD_GroupHome_Status;N:\\TechTemp\\BlakeT\\Work\\GroupHome_Helpdesk43929\\GroupHome.gdb\\PD_GroupHome_Type", TEMP_gdb)
Thats the only one which is a script tool, which might have something to do with it.