Create Runtime Content tool in Python script

4571
7
11-11-2015 05:53 PM
JanaPfefferova
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!

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

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

0 Kudos
JanaPfefferova
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

0 Kudos
AnttiKajanus1
Occasional Contributor III
0 Kudos
FreddieGibson
Occasional Contributor III

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)
JanaPfefferova
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.

0 Kudos
FreddieGibson
Occasional Contributor III

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.

0 Kudos
JanaPfefferova
New Contributor

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

0 Kudos