Select to view content in your preferred language

Export layer packages from outside ArcMap using python.

1193
4
07-22-2011 07:59 AM
SamuelBoisvert
Emerging Contributor
Hi,
I was wondering if there is a way to use a python script from outside ArcMap that export a series of layer packages to a given folder. I built the following script mainly with Model Builder inside the .mxd from which I want to export the packages, as a matter of fact that script works well when I start it inside my ArcMap document.... The problem is that I want it to run from outside ArcGIS. So I have to find a way to reference (inside my script) the mxd from which I want the layers package to be exported from. Does anyone have any solution on how I could do that?
Hereâ??s my python script:

# ---------------------------------------------------------------------------
# JavaScript.py
# Created on: 2011-07-13 08:38:45.00000
#   (generated by ArcGIS/ModelBuilder)
# Description: To be added...
# Auteur: Samuel Boisvert
# ---------------------------------------------------------------------------
# Import arcpy module
import arcpy

# Set the workspace
arcpy.env.workspace = "c:/LPK"

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Local variables
Layer = "Layer"
Paquetage = "C:\\LPK\\LPK.lpk"

# Process: Package Layer
arcpy.PackageLayer_management("Layer", Paquetage, "PRESERVE", "CONVERT_ARCSDE", "DEFAULT", "ALL", "ALL", "CURRENT")


Thank you
Best regards,

Samuel Boisvert
Tags (2)
0 Kudos
4 Replies
ChrisFox3
Frequent Contributor
Samuel,

You would want to something like the following to get a reference to the layer in the map document.

mxd = arcpy.mapping.MapDocument(r"C:\Data\MyMapDoc.mxd")
lyr = arcpy.mapping.ListLayers(mxd, "LayerName")[0]
0 Kudos
SamuelBoisvert
Emerging Contributor
Hi Chris!,
thanks for the answer! As you suggested I did some changes in my code. As I run the code, the layer package failed to proceed (error 99999). I looked for common errors like "layer without a description" but everything is ok. I succeeded exporting manually the layer to a package so it's probably something missing in the script! Any idea?

Here's the python script i've been using:

# Import arcpy module
import arcpy

# Set the workspace
arcpy.env.workspace = "c:/LPK"

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Local variables:
mxd = arcpy.mapping.MapDocument(r"C:\LPK\Paquetage_de_Couche.mxd")
lyr = arcpy.mapping.ListLayers(mxd, "Test")[0]
Package = r"C:\LPK\Packaged_Layer.lpk"

# Process: Package Layer
arcpy.PackageLayer_management(lyr, Package, "PRESERVE", "CONVERT_ARCSDE", "DEFAULT", "ALL", "ALL", "CURRENT")


thank's in advance!

Best regards,

Samuel Boisvert
0 Kudos
ChrisFox3
Frequent Contributor
Hi Samuel,

Sorry about that but forgot one additional step. It is not possible to just pass in a layer object as an input to the geoprocessing tool, first you need to convert it toa layer file and then pass in the layer file path. Below is a sample:

# Import arcpy module
import arcpy

# Set the workspace
arcpy.env.workspace = "c:/LPK"

# Overwrite pre-existing files
arcpy.env.overwriteOutput = True

# Local variables:
mxd = arcpy.mapping.MapDocument(r"C:\LPK\Paquetage_de_Couche.mxd")
lyr = arcpy.mapping.ListLayers(mxd, "Test")[0]
lyrFilePath = r"C:\LPK\myLayerFile.lyr"
lyr.saveACopy(r"C:\LPK\myLayerFile.lyr")
Package = r"C:\LPK\Packaged_Layer.lpk"

# Process: Package Layer
arcpy.PackageLayer_management(lyrFilePath, Package, "PRESERVE", "CONVERT_ARCSDE", "DEFAULT", "ALL", "ALL", "CUR

#Optionally delete layer file when done
arcpy.Delete_management(lyrFilePath)
0 Kudos
SamuelBoisvert
Emerging Contributor
Hi Chris,
            everything is working well right now!

Thank's a lot! Very usefull help!

Samuel
0 Kudos