Select to view content in your preferred language

Failed when running arcpy.StageService_server('','' )

8292
3
03-21-2014 01:09 AM
sainanzhang1
New Contributor
Hi guys, I am a Chinese young girl learning how to use Python to publish the map service. I have an error when I was trying to Stage Service (Server). The following is my code:
env=r"C:/test"
#env=r"G:/Data/"
mxd=env+r"/WJZT-20140312.mxd"

mapDoc = arcpy.mapping.MapDocument(mxd)
service = 'test'
sddraft = service + r'.sddraft'
sd = service + r'.sd'

analysis = arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, 'ARCGIS_SERVER')
analysis = arcpy.mapping.AnalyzeForSD(sddraft)
analysis = arcpy.StageService_server(sddraft, sd)

I got an error said:
"Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Program Files\ArcGIS\Desktop10.1\arcpy\arcpy\server.py", line 1140, in StageService
raise e
ExecuteError: ERROR 999999: Error executing function.
Failed to execute (StageService)."

I dont know where is wrong. Can anyone have the same issue before? Or can provide me a help? Please. Thank you.
Tags (2)
0 Kudos
3 Replies
JakeSkinner
Esri Esteemed Contributor
I believe this may be a bug.  I was able to reproduce using ArcGIS 10.2.1.  I would recommend contacting Tech Support so they can log this bug and find a potential workaround.
0 Kudos
BradyHoak
Esri Contributor

I had the exact same issue. The sddraft was created on disk but the stageservice function failed. I believe the error lies in that the path names need to be complete and with double forward slashes for that function to work. I made a model in desktop using the stageservice tool (which required my sddraft) and then the upload service definition tool, exported that to python to see the working syntax, and embedded into my code. Here is the working function that makes a service draft, stages, and uploads it.

def createDraftSDandPublishService():

  # mxd and connection to server

  mapDoc = arcpy.mapping.MapDocument('Incidents.mxd')

  serverConnectionInDesktop = 'GIS Servers\\arcgis on Server1'

  #service draft staging location

  sddraft = "C:\\databaseimport\\Incidents.sddraft"

  sd = "C:\\databaseimport\\Incidents.sd"

  #publishing options

  existingServerFolder = "Services_1505"

  copyData = 'True'

  #metadata for service

  serviceName = 'Incidents'

  tags = 'incidents'

  summary = 'Incident test'

  print("start")

  print("create sddraft....")

  # create service definition draft

  analysis = arcpy.mapping.CreateMapSDDraft(mapDoc,

                                          sddraft,

                                          serviceName,

                                          'ARCGIS_SERVER',

                                          serverConnectionInDesktop,

                                          copyData,

                                          None,

                                          summary,

                                          tags)

  # stage and upload the service if the sddraft analysis did not contain errors

  if analysis['errors'] == {}:

      print("no errors")

      # Execute StageService

      print("staging....")

      # Process: Stage Service

      arcpy.StageService_server(sddraft, sd)

   

      print("uploading....")

      # Process: Upload Service Definition

      arcpy.UploadServiceDefinition_server(sd,

                                        serverConnectionInDesktop,

                                        serviceName,

                                        "",

                                        "EXISTING",

                                        existingServerFolder,

                                        "STARTED",

                                        "USE_DEFINITION",

                                        "NO_SHARE_ONLINE",

                                        "PRIVATE",

                                        "NO_SHARE_ORGANIZATION",

                                        "")

  else:

      print("errors")

      # if the sddraft analysis contained errors, display them

      print analysis['errors']

NathanHeickLACSD
Frequent Contributor

Hello Sainan,

Reviewing your code, env is a variable you created in your Python script.  As used, it only helps you build the mxd path.  It does not set the workspace environment variable.  Environment variables are set through the env property of the arcpy module (arcpy.env).  The current workspace is set through the workspace property of arcpy.env or arcpy.env.workspace.  If you replace env with arcpy.env.workspace, your code works.

Apparently, setting the current workspace or using absolute paths is required.  It has nothing to do with which slashes you use.

On a similar note, when you put r before a string, it's called a raw string.  It causes the Python interpreter to use different rules for interpreting backslashes.  For the most part, it stops trying to interpret them as the start of escape sequences.  For example, r"\n" is a backslash and the letter n, whereas "\n" is the newline character written using its escape sequence.  I only put r in front of paths in order to keep them in the standard windows path notation, for example r"C:\Test".  r"C:/Test" is the exact same as "C:/Test", so there is no point in adding it.  Strings like r".sddraft" don't need the r either.  It is shorter just to use ".sddraft".

Also, arcpy.mapping.CreateMapSDDraft analyzes the service definition draft and returns the analysis results.  You should check those results that you captured in the variable analysis before proceeding to stage the service.  The call to arcpy.mapping.AnalyzeForSD is not needed after that.  You would call that if you already had a service definition file, but you hadn't analyzed it.  Finally, arcpy.server.StageService returns the path to the service definition file, so if you aren't using it, then storing it in the variable analysis isn't necessary.

0 Kudos