Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
    
Should your import toolbox section have an extra '\'? arcpy.ImportToolbox(toolboxLocation+"\\ApplyDefaultTopology.tbx") –  Fezter Dec 7 '12 at 1:03
    
Generally not when the '\' is included inside the double quotes. I did try arcpy.ImportToolbox(toolboxLocation+"\\"+"ApplyDefaultTopology.tbx") and it did not work either. If i use gp.AddToolbox(toolboxLocation+"\ApplyDefaultTopology.tbx") it works fine. Very strange. –  danagerous Dec 7 '12 at 4:04
    
Apparently this should not be a problem in ArcGIS 10.1 or 10.0 with SP5. I'm waiting to hear from our IT department which SP we have installed on our servers which may eliminate the use of our former work around. I'll post as soon as I get info and test. –  danagerous Dec 7 '12 at 18:19
    
Well it appears that we do indeed have ArcGIS 10 SP5 on our servers which eliminates the need to use the work around that was created to avoid the bug pre-SP5. I tested this to confirm and the script ran without error and will undoubtedly run on our new 10.1 servers as well. Cheers! –  danagerous Dec 7 '12 at 21:50
    
I'm still getting the error in ArcCatalog which I'll report back on when I figure it out. Sounds like a Python problem from what i've read so far. –  danagerous Dec 11 '12 at 19:06
add comment

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.

share|improve this answer
    
Thanks for the tip with the paths. However the tip regarding the importToolbox method is not the problem as I always create an alias for my toolbox. It appears that the ImportToolbox 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 the AddRuleToTopology method and now they fixed that problem in 10.0 and a new problem is the ImportToolbox method doesn't work. Looking for workaround...stay tuned –  danagerous Dec 10 '12 at 19:27
    
Works fine for me. /shrug –  blah238 Dec 10 '12 at 21:04
    
Are you using ArcGIS 10.0 or ArcGIS 10.1? –  danagerous Dec 10 '12 at 23:36
    
I'm using 10.1 SP1. –  blah238 Dec 10 '12 at 23:43
    
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 Dec 11 '12 at 19:13
add comment
up vote 2 down vote accepted

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:

  1. I had an offending bit of code which overwrote the selected feature class throwing the script. I removed that and all is good.
  2. 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>)
share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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