1

I tried to automate the overwriting of an existing map service using a stand-alone Python script.

I mirrored the Esri's sample scripts. But when I ran it, the second print statement was never executed. However, when I ran the same script from the Python window inside ArcMap, the script ran as expected.

import arcpy

wrkspc = 'C:/GIS_Projects/'
mxd_name = 'Test.mxd'
mxd = arcpy.mapping.MapDocument(wrkspc + mxd_name)
server_con = 'GIS Servers/arcgis on myserver_6443 (admin)'
service = 'Test'
sddraft = wrkspc + service + '.sddraft'
sd_file = wrkspc + service + '.sd'
print 'local variables set'

analysis = arcpy.mapping.CreateMapSDDraft(mxd, sddraft, service, 'ARCGIS_SERVER', server_con, True, None, 'test', 'test')
print 'SDdraft file created'
  • To me it looks like a standalone script python can't make sense of 'GIS Servers/arcgis on myserver_6443 (admin)' as a path. Change it to a full path name or copy the connection file into 'C:/GIS_Projects' Also try wrapping your code in a try catch clock. It will help with debugging. – Dowlers Aug 17 '16 at 19:16
  • I tried to use r'C:\Users\<username>\AppData\Roaming\ESRI\Desktop10.4\ArcCatalog\arcgis on myserver_6443 (publisher).ags' as the connection file. But that didn't solve the problem :( – lesi Aug 17 '16 at 19:20
  • add some error handling to see what the error message being given is: docs.python.org/2/tutorial/errors.html – Dowlers Aug 17 '16 at 19:24
  • also try setting overwrite to true arcpy.env.overwriteOutput = True – Dowlers Aug 17 '16 at 19:25
  • 1
    Based on your connectionfile, your server is setup on port 6443, a secure server. Do you have a proper security certificate installed on the server? If not, when the CreateMapSDDraft command runs outside ArcMap it'll get back a security warning dialog that it cannot do anything with. Inside ArcMap you would have got this as well, but clicked "ignore" or "ok". Do you get that security warning in ArcMap in a fresh session? – KHibma Aug 17 '16 at 20:02
3

When I run the following it produces the .sddraft file as expected. I made a couple of changes to your script

  • I used os.path.join to create my folder paths (which possibly didn't change anything in the outputs but just ensures folders are constructed properly)
  • Added a .ags on the end of the server_con
import arcpy, os

wrkspc = r'C:\GIS_Projects'
mxd_name = 'Test.mxd'
mxd = arcpy.mapping.MapDocument(os.path.join(wrkspc, mxd_name))
server_con = r'GIS Servers\arcgis on myserver_6443 (admin).ags'
service = 'Test'
sddraft = os.path.join(wrkspc, "{}.sddraft".format(service))
sd_file = os.path.join(wrkspc, "{}.sd".format(service))
print 'local variables set'

analysis = arcpy.mapping.CreateMapSDDraft(mxd, sddraft, service, 'ARCGIS_SERVER', server_con, True, None, 'test', 'test')
print 'SDdraft file created'

This printed both print lines, and output a Test.sddraft file.

I also tested without the .ags and the script still ran as expected. You may need to confirm your folders and files exist as per your script.

  • Thanks for sharing the script. I tested it on my end. The second print statement still doesn't get executed. But when I ran the same script on another machine, it ran as expected. Thinking maybe this is related to some settings on the first machine. Still need to figure out what that is. – lesi Aug 17 '16 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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