Create Runtime Content tool in Python script

3799
7
11-11-2015 05:53 PM
Highlighted
New Contributor

Hi,

I need to automate the process of creating runtime geodatabase. I have a working model which use the Create Runtime Content tool. It works without any errors.

I exported this model to python script and it failed to execute.

So I tried to run that model from toolbox by using arcpy.ImportToolbox and it still wouldn't execute.

If I just copy this line arcpy.CreateRuntimeContent_management("C:\\Jana\\final.mxd", "C:\\Jana\\MobileData", "", "", "DEFAULT", "FEATURE_AND_TABULAR_DATA", "NON_OPTIMIZE_SIZE", "ONLINE", "PNG", "1", "") into python window in ArcCatalog it worked fine without any errors.

This is my stand-alone script for running the model:

import arcpy

arcpy.ImportToolbox("C:\Jana\Jana.tbx")

try:

    arcpy.Jana.Content()

except arcpy.ExecuteError:

    print(arcpy.GetMessages(2))

My toolbox alias is "Jana" and model name (as well as label) is "Content". I have also try to use arcpy.Content_Jana with the same "Failed to execute".

Any thoughts?

Thanks!

Reply
0 Kudos
7 Replies
Highlighted
MVP Esteemed Contributor

the import toolbox line should have double backslashes, single foreslashes or an r in front of it for raw formatting a single backlash is interpreting the next character as an escape sequence.  It is a python thing

Reply
0 Kudos
Highlighted
New Contributor

Hi Dan,

Thanks for your reply.

I have copied wrong version but I had tried also with “r” and with double back slashes and single front slashes before. It’s not causing the issues. The error is the same “Error executing function” no matter which slashes I use.

Jana

Reply
0 Kudos
Highlighted
Regular Contributor II
Reply
0 Kudos
Highlighted
Regular Contributor II

I can see that you're running your code against the 64-bit install of python. I'd be curious to find out if your code works against the 32-bit install of python. I won't be able to test this against the 64-bit install until Monday, but everything works fine on my machine.

Could you try running the attached script? I've included a map package that the code can run against to create the runtime content. I've pasted the code below as well. The code is designed to extract a map document from the map package and use it to create the runtime content.

import arcpy
import subprocess
from os import path, walk

def ExtractMpk(mpkPath):
    outFolder = mpkPath.replace(".mpk", "_extracted")
   
    if arcpy.Exists(outFolder):
        arcpy.management.Delete(outFolder)
       
    arcpy.management.ExtractPackage(mpkPath, outFolder)
   
    mxdPaths = []
    for root, dirs, files in walk(outFolder):
        for name in files:
            if name.endswith(".mxd"):
                return path.join(root, name)
   
    return None

def ValidateMxd(mxdPath, filler="Test"):
    mxd = arcpy.mapping.MapDocument(mxdPath)
    empty = (None, "", " ")
   
    for attr in ("author", "credits", "description", "summary", "tags", "title"):
        if getattr(mxd, attr).strip() in empty:
            setattr(mxd, attr, filler)
   
    naLayer = False   
   
    for df in arcpy.mapping.ListDataFrames(mxd):
        if df.description.strip() in empty:
            df.description = filler
        for lyr in arcpy.mapping.ListLayers(mxd, data_frame=df):
            if lyr.description.strip() in empty:
                lyr.description = filler
            if lyr.isNetworkAnalystLayer:
                naLayer = True
               
    mxd.save()
   
    return naLayer
           

def CreateRtContent(mxdPath, naLayer=False):
    outFolder = mxdPath.replace(".mxd", "_rtc")
   
    if arcpy.Exists(outFolder):
        arcpy.management.Delete(outFolder)
   
    params = {"extent":"MAXOF", "optimize":"NON_OPTIMIZE_SIZE", "options":"FEATURE_AND_TABULAR_DATA"}   
    if naLayer: params["options"].extend(["NETWORK_DATA"])
   
    arcpy.management.CreateRuntimeContent(mxdPath, outFolder, **params)
   
    # Open the folder where the runtime content is created.
    subprocess.Popen("explorer {0}".format(outFolder))

if __name__ == "__main__":
    srcPath = path.dirname(__file__)
    mpkPath = path.join(srcPath, "MapPackage.mpk")
   
    # Extract the included map package
    mxdPath = ExtractMpk(mpkPath)
   
    # Validate the map document
    naLayer = ValidateMxd(mxdPath)
   
    # Create Runtime Content
    CreateRtContent(mxdPath, naLayer)

Highlighted
New Contributor

Hi Freddie,

You’re right.

I have installed pywin32 (32 bit) and it works. It also works in pyscripter but I needed pywin to be able to automate it in task scheduler.

Thanks a lot.

Reply
0 Kudos
Highlighted
Regular Contributor II

In task scheduler are you explicitly setting the 32 bit python executable to run your script or are you using the default for the system? I would highly suggest that you explicitly set the python executable if your default isn't the 32bit instance. I'm assuming the default on your machine is the 64 bit instance.

Reply
0 Kudos
Highlighted
New Contributor

I'm explicitly setting the 32 bit python to run it. It didn't work with the default 64 bit instance.

Reply
0 Kudos