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.
3 Answers 3
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
.
-
Thanks for the tip with the paths. However the tip regarding the
importToolbox
method is not the problem as I always create analias
for my toolbox. It appears that theImportToolbox
method is plagued with bugs. There are many listed on the ESRI support site. It's crazy considering we had to find a work around for the Boundary Must Be Covered by Boundary of (Area-Area) rule which did not work with theAddRuleToTopology
method and now they fixed that problem in 10.0 and a new problem is theImportToolbox
method doesn't work. Looking for workaround...stay tuneddanagerous– danagerous2012年12月10日 19:27:23 +00:00Commented Dec 10, 2012 at 19:27 -
Works fine for me. /shrugblah238– blah2382012年12月10日 21:04:25 +00:00Commented Dec 10, 2012 at 21:04
-
Are you using ArcGIS 10.0 or ArcGIS 10.1?danagerous– danagerous2012年12月10日 23:36:30 +00:00Commented Dec 10, 2012 at 23:36
-
-
I'll test on our 10.1 server when time permits. It's only a test server right now and not launched for our entire GIS community. ESRI may have fixed these problem with the new version. Thanks for your help.danagerous– danagerous2012年12月11日 19:13:58 +00:00Commented Dec 11, 2012 at 19:13
Here is an update from my previous problem.
The bug is indeed fixed. Using the work around suggested is not necessary with ArcGIS 10.0 SP5 and newer.
There were two problems with my python
/arcpy
script:
- I had an offending bit of code which overwrote the selected feature class throwing the script. I removed that and all is good.
- The
AddRuleToTopology_management
tool needed a parameter filled with the '#' place holder which I neglected to put in for subtype.
I had this:
arcpy.AddRuleToTopology_management(<topologyName>,'Must Be Properly Inside (Point-Area)',<featureClass>,<boundaryFC>)
When I should have had this for the subtype parameter as per the syntax in help:
arcpy.AddRuleToTopology_management(<topologyName>,'Must Be Properly Inside (Point-Area)',<featureClass>,'#',<boundaryFC>)
It appears that using the AddRuleToTopology()
method for the Must Be Inside (Line-Area) rule (which is a new rule in 10.0) requires a work around calling a model to do the work; the same as the Must Be Covered by Feature Class of (Area-Area) rule work around pre ArcGIS 10.0 (SP5).
It turned out that my custom model being called by arcpy.ImportToolbox()
method was missing the new rule under the rule type argument within the model itself. I ran the script again and it failed. So instead of passing the input name as a string variable like so:
toolboxLocation = os.path.join(os.path.dirname(sys.argv[0]), "ApplyDefaultTopology.tbx")
arcpy.ImportToolbox(toolboxLocation)
I omitted the variable toolboxLocation
and simply used the string directly as input like so and the script ran successfully:
arcpy.ImportToolbox(os.path.join(os.path.dirname(sys.argv[0]), "ApplyDefaultTopology.tbx")
)
The ESRI bug for the ImportToolbox()
method is what tipped me off to this problem.
I also managed to fix the undefined variable problem. See this post to fix the problem with a simple ctrl+1
in the windows environment to have Eclipse ignore the error.
Explore related questions
See similar questions with these tags.
arcpy.ImportToolbox(toolboxLocation+"\\ApplyDefaultTopology.tbx")
'\'
is included inside the double quotes. I did tryarcpy.ImportToolbox(toolboxLocation+"\\"+"ApplyDefaultTopology.tbx")
and it did not work either. If i usegp.AddToolbox(toolboxLocation+"\ApplyDefaultTopology.tbx")
it works fine. Very strange.