[GIS] Why does arcpy.ImportToolbox() not work in ArcGIS 10 (SP5) when calling custom model for topology

arcgis-10.0arcgis-9.3arcpyeclipsepython

I have an arcpy/python script using ArcGIS10 and using a custom toolbox and model. The toolbox has an alias of defaultTopo and my model within the toolbox is called AddRuleToTopologyModel. This model was created because there is a known bug when calling the AddRuleToTopology_management tool for the Must Be Covered by Feature Class of (Area-Area) rule from python and the work around is to use this model. I'm simply updating my script from 9.3 to 10.0.

The code goes like this:

# Defining location of toolbox which is in the same location as the script
toolboxLocation = sys.argv[0][:sys.argv[0].rfind('\\')]

# Importing custom toolbox
arcpy.ImportToolbox(toolboxLocation+"\ApplyDefaultTopology.tbx")

# Calling custom model for polygon feature class - not all the variables are posted here
arcpy.AddRuleToTopologyModel_defaultTopo(featureDataset+"\\"+topologyName,'Must Be Covered By Feature Class Of (Area-Area)',featureClass,featureDataset+"\\"+boundaryFC)

Now if i run the script from Eclipse it does not pick up my featureDataset location I use as argv1 in the script throwing the error I have in place.

If I try and run the script from ArcCatalog I get the following error:

<type 'exceptions.AttributeError'>: 'module' object has no attribute 'exists'

Eclipse also tells me that arcpy.AddRuleToTopologyModel_defaultTopo() is an undefined variable when trying to only use arcpy.

The strange thing is if i use gp.arcgisscripting(9.3) with gp.AddToolbox() to replace arcpy.ImportToolbox() the script runs fine from Eclipse and ArcCatalog because arcpy still supports the 9.3 geoprocessing object. I've been asked to remove all references to the arcgisscripting module and make the new script pure arcpy to function on our new servers.

Thoughts?

Thanks in advance.

Best Answer

The string manipulation you are doing with pathnames is very fragile.

The functions in the os.path module are designed to take the guesswork out of path manipulations and make much more sense to use here rather than string manipulation.

Rewritten to use os.path functions join (joins path elements) and dirname (gets the parent directory's path):

import os

# Defining location of toolbox which is in the same location as the script
toolboxLocation = os.path.join(os.path.dirname(sys.argv[0]), "ApplyDefaultTopology.tbx")

# Importing custom toolbox
arcpy.ImportToolbox(toolboxLocation)

# Calling custom model for polygon feature class - not all the variables are posted here
arcpy.AddRuleToTopologyModel_defaultTopo(os.path.join(featureDataset, topologyName), 'Must Be Covered By Feature Class Of (Area-Area)', featureClass, os.path.join(featureDataset, boundaryFC))

Additionally take note of the help on module_name argument of the ImportToolbox function:

If the toolbox does not have an alias, the module_name is required.

Tip:

The best practice is to assign an alias when first creating the toolbox rather than using ImportToolbox to assign a temporary one that will only be applicable in Python.

If you have not assigned an alias to the Toolbox previously then it will not be found by ImportToolbox, unless you specify a module_name.

Related Question