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.

My question is similar to this post: Adding Layer in current session using ARCPY . My script works in ArcMap python console but not as a script in toolbox.I am running ArcGIS 10.0 SP4. The script is given below, and the error says it fails at line 3, "CreateObject cannot open map document"

import arcpy
import arcpy.mapping as map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
updateLayer = arcpy.mapping.ListLayers(mxd, "SectionClip",df)[0]
arcpy.MakeFeatureLayer_management(updateLayer,"SectionInner2")
sourceLayer = arcpy.mapping.ListLayers(mxd,"SectionInner",df)[0]
newupdateLayer = arcpy.mapping.ListLayers(mxd, "SectionInner2",df)[0]
arcpy.ApplySymbologyFromLayer_management(newupdateLayer,sourceLayer)
newupdateLayer.showLabels = True
for lyr in map.ListLayers(mxd):
    if lyr.name == "SectionInner2":
        lyr.showLabels = True
        for lblclass in lyr.labelClasses:
            lblclass.expression = '"%s" & [SECTION] & "%s"' % ("<CLR red='0' green='0' blue='0'>""<FNT size='12'>""<BOL>", "</BOL>""</FNT>""</CLR>")
            lblclass.showClassLabels = True

arcpy.RefreshActiveView()
mxd.save()

Will appreciate any suggestions/fixes, anything other than upgrading to 10.1 world of python add-in wizard. No plans of going there any time soon.

Thanks in advance,

-Dee

share|improve this question
4  
Do you have the "Always run in foreground" and "Run Python script in process" options checked? –  blah238 Nov 2 '12 at 0:21
2  
I think the reason @blah238 asks his question is described well in the online help under the heading Reference a map document on disk or use the CURRENT keyword within ArcMap –  PolyGeo Nov 2 '12 at 4:58
    
Did @blah238 suggestion help you? If so, then let us know so that can be transferred to an Answer that you can Accept. –  PolyGeo Nov 22 '12 at 8:24
    
Checked both "Always run in foreground" and "Run Python script in process". Still not working. btw thanks PolyGeo for the link, helped me understand what the checks were for. –  techtonic Nov 30 '12 at 21:33
add comment

1 Answer

The only thing I see in your code that could cause this problem is that you are setting the MXD value to "CURRENT", which is fine... IF you have an MXD open (executing the script tool from within ArcMap).

I was able to cause your code to fail with the same response if I tried executing the code form the stand-alone ArcCatalog application. Is that what you were doing?

Screenshot using ArcCatalog to execute a Script Tool enter image description here



If trying to run the script from stand-alone ArcCatalog was your problem, you have two options to run your script.
1. Run the Script Tool from the Catalog tab within the ArcMap enter image description here

2. Application Set the MXD parameter as a path to a valid MXD

arcpy.mapping.MapDocument(r"c:\path\to\your\map.mxd")




You can read more details about the use of the arcpy.Mapping.MapDocument class here

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.