0

Every time I run this script now, I get an error on the line with the MakeFeatureLayer line. Sorry for all the questions! I'm really new to Python.

import arcpy, os

arcpy.env.workspace = r'C:\pathway to gdb'
fcs = arcpy.ListFeatureClasses()

mxd = arcpy.mapping.MapDocument(r'C:\pathway to mxd')
DF = arcpy.mapping.ListDataFrames(mxd,'*')[0]


for feature in fcs:
    inputlayer = 'C:\pathway to gdb' 
    outlayer = os.path.join(fcs, 'feature'+'.lyr')

    arcpy.MakeFeatureLayer_management(inputlayer,outlayer)

    layer = arcpy.mapping.Layer(mxd)
    arcpy.mapping.AddLayer(DF,layer,"AUTO_ARRANGE")
  • can you run it from the "Python" window in ArcMap. I do that a lot with something like ... execfile(r"C:\myfolder\myfile.py") – jbchurchill Mar 26 '15 at 18:50
  • Does this question help? gis.stackexchange.com/q/21906/43 – Richard Morgan Mar 26 '15 at 18:51
  • The question you posted Richard I had stumbled across before. However, how do you run this process in the foreground rather than the background? Also, this needs to be run in IDLE. – user48149 Mar 26 '15 at 19:39
2

Answer to your original question:

There is no "CURRENT" MXD when running outside of ArcMap. Use mxd = arcpy.mapping.MapDocument(r'C:\full\path\to.mxd') instead.

Answer to your edited question:

You can't create a Layer object from a MapDocument object (i.e. layer = arcpy.mapping.Layer(mxd)). Do something like this instead:

for fc in fcs:
    layer = arcpy.mapping.Layer(fc)
    arcpy.mapping.AddLayer(DF,layer,"AUTO_ARRANGE")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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