0

I'm trying to create a layer package from a feature layer (whose data source is a feature class) using arcpy. However, I keep getting the following error:

Object: Error in executing tool

Here's the code I'm using:

import arcpy

arcpy.env.overwriteOutput = True
arcpy.env.workspace = "D:/Data/"
mxd = arcpy.mapping.MapDocument("CURRENT")
print "environments set"

lpk = "layerPackage.lpk"
summary = "A summary"
tags = "tag1", "tag2"
print "layer package variables defined"

dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
layers = arcpy.mapping.ListLayers(mxd, "", dataFrame)
print layers

for lyr in layers:
    try:
        if lyr.name == "Layer_I_Want":
            print "converting Layer_I_Want to lpk"
            arcpy.PackageLayer_management(lyr, lpk, "CONVERT", "PRESERVE_ARCSDE", 
            "#", "ALL", "ALL", "CURRENT", "#", summary, tags)
            print "lpk created"
    except Exception as e: print(e)

mxd.save()

How do I fix it?

I've tried different combinations of "CONVERT"/"PRESERVE" but still get the same error.

3
  • I'm not sure, but one thing I see is that you're allowing overwrite outputs before setting your workspace, is this keeping your outputs from being written due to duplicate files? Commented Dec 20, 2016 at 16:10
  • Per the doc, the first CONVERT is not relevant when using SDE data. Use "CONVERT_ARCSDE". Also, try making lpk a full path using os.path.join (workspace,lpkName.lpk) desktop.arcgis.com/en/arcmap/10.3/tools/data-management-toolbox/… Commented Dec 21, 2016 at 14:07
  • So i made the changes you guys suggested but still received the same error. However, I did manage to get it to work simply by combining tag1 and tag2 as a single string. Below is my final code for reference.
    – Cat
    Commented Jan 3, 2017 at 18:02

1 Answer 1

0

Working code:

import arcpy
import os

arcpy.env.workspace = "D:/Data/"
workspace = arcpy.env.workspace
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
print "environments set"

lpkName = "layerPackage"
lpk = os.path.join(workspace+lpkName+".lpk")
summary = "A summary"
tags = "tag1, tag2"
print "layer package variables defined"

dataFrame = arcpy.mapping.ListDataFrames(mxd, "*")[0]
layers = arcpy.mapping.ListLayers(mxd, "", dataFrame)

for lyr in layers:
  try:
    if lyr.name == "Layer_I_Want":
        print "converting Layer_I_Want to lpk"
        arcpy.PackageLayer_management(lyr, lpk, "PRESERVE", "CONVERT_ARCSDE", 
        "#", "ALL", "ALL", "CURRENT", "#", summary, tags)
        print "lpk created"
except Exception as e: print(e)

mxd.save()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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