<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Overwrite a map service using a python script outside of Pro in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688722#M75138</link>
    <description>&lt;P&gt;Similar to what others have mentioned, I would suggest:&lt;/P&gt;&lt;P&gt;1. Make sure you have opened Pro on the machine the script is running on, and configure licensing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Make sure your script is using the Python that comes with Pro, rather than the one that comes with ArcMap.&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. If you haven't already rewritten the script to work with how Pro publishes services, you'll need to do that - it's completely different.&amp;nbsp; I had to go through this a few years ago, and here's some code from my script that republishes a service.&amp;nbsp; NOTE: this is used for a standalone, non-federated ArcGIS Server. So ArcGIS Portal is not involved. I don't know if this code represents the best or most elegant way of publishing a service, but it's what I managed to piece together, and it works for me. It's a function that the script calls at some point. It also produces a message that is part of the email that the script sends upon finishing.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def create_sd(path_aprx, aprx_map_name, project_dir, conn_string, folder, feature_count, overwrite=False):
    """ Create an sd file from an aprx. If feature_service is True, the
        sd is configured to be published as a feature service on AGOL."""
    # Overwrite output for GP tools
    arcpy.env.overwriteOutput = True
    # Check that input file is an .aprx
    if path_aprx.endswith(".aprx"):
        # Reference map to publish
        aprx = arcpy.mp.ArcGISProject(path_aprx)
        m = aprx.listMaps(aprx_map_name)[0]
        # Get service draft name from the input .mxd file name
        service_def_draft_name = os.path.basename(path_aprx).replace(".aprx", ".sddraft")
        # Get service name
        service_name = "".join(service_def_draft_name.split(".")[0])
        # Path to service draft definition file
        path_service_draft_def = os.path.join(project_dir, service_def_draft_name)
        # Create MapServiceDraft and set metadata and server folder properties
        sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
        sddraft.targetServer = conn_string
        sddraft.summary = "Write your item summary here"
        sddraft.credits = "Your organization"
        sddraft.description = "Description of your item goes here."
        sddraft.serverFolder = folder
        sddraft.tags = "your tags"
        sddraft.overwriteExistingService = True
        # Create Service Definition Draft file
        sddraft.exportToSDDraft(path_service_draft_def)
        # Create the name for the service definition
        service_def_name = "{}withtable.sd".format(service_name)
        # Path to the service definition on disk
        path_service_def = os.path.join(project_dir, service_def_name)
        # Stage the service definition file
        arcpy.StageService_server(path_service_draft_def, path_service_def)
        try:
            # Upload the service definition to publish the service to ArcGIS Server
            arcpy.UploadServiceDefinition_server(path_service_def, conn_string)
            # Return true if service published
            return True
        except:
            # Create message if publish service fails. Message used in email sent by script.
            sd_mess = "*** Unable to Publish Service. Service needs to be manually restarted. ***"
            # Return message
            return sd_mess
    else:
        # Create message if input file is not .aprx. Message is used in email sent by the script.
        sd_mess = "Input Path Must Point to an .aprx"
        # Return message
        return sd_mess&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 05 Mar 2026 18:08:01 GMT</pubDate>
    <dc:creator>AllenDailey1</dc:creator>
    <dc:date>2026-03-05T18:08:01Z</dc:date>
    <item>
      <title>Overwrite a map service using a python script outside of Pro</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688526#M75134</link>
      <description>&lt;P&gt;I am trying to write a python script that I can run outside of Pro that overwrites a map service on an ArcGIS Enterprise server.&amp;nbsp; I have a python script that uses a mxd and can overwrite a map service that was published from ArcMap.&amp;nbsp; How do I get this script to work using a Pro project instead of a mxd?&amp;nbsp; I've changed the script to point to my aprx file.&amp;nbsp; When I try to run the script from a command prompt, I get a 'Product License has not been initialized' error message.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2026 22:10:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688526#M75134</guid>
      <dc:creator>meb323</dc:creator>
      <dc:date>2026-03-04T22:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite a map service using a python script outside of Pro</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688558#M75135</link>
      <description>&lt;P&gt;Off the top of my head, that almost sounds like your script is actually pinging a different Python install than the one tied to your ArcPro login.&amp;nbsp; My memory is a little fuzzy on the specifics at the moment, but I vaguely remember running into something like this back when I first transitioned from ArcMap to ArcPro, because something ran in the former's python environment rather than the latter's.&lt;/P&gt;</description>
      <pubDate>Wed, 04 Mar 2026 23:51:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688558#M75135</guid>
      <dc:creator>MErikReedAugusta</dc:creator>
      <dc:date>2026-03-04T23:51:59Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite a map service using a python script outside of Pro</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688573#M75136</link>
      <description>&lt;P&gt;Can you post the code?&amp;nbsp; Are you using a Named User license and have opened Pro with that Named User license you are running the script on?&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2026 01:18:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688573#M75136</guid>
      <dc:creator>MichaelVolz</dc:creator>
      <dc:date>2026-03-05T01:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite a map service using a python script outside of Pro</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688658#M75137</link>
      <description>&lt;P&gt;We have python scripts running off a server using task scheduler. When we ran into this we had to open ArcGIS Pro on the server and that fixed it but we discovered that only solved the issue for 90 days (as posted on other ESRI community posts). We installed a single use license on that server too (also based on resolutions of the issue based on ESRI Community posts).&amp;nbsp;&lt;/P&gt;&lt;P&gt;As far as overwriting from an .aprx vs .mxd, I found I had to publish using Pro first that would create .sd and .sddraft files. Once you do that, then I could overwrite. Here's what I used as code examples with success:&amp;nbsp;&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/3.3/arcpy/sharing/featuresharingdraft-class.htm#GUID-8E27A3ED-A705-4ACF-8C7D-AA861327AD26" target="_self"&gt;https://pro.arcgis.com/en/pro-app/3.3/arcpy/sharing/featuresharingdraft-class.htm#GUID-8E27A3ED-A705-4ACF-8C7D-AA861327AD26&lt;/A&gt;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 05 Mar 2026 14:55:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688658#M75137</guid>
      <dc:creator>ShariF</dc:creator>
      <dc:date>2026-03-05T14:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: Overwrite a map service using a python script outside of Pro</title>
      <link>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688722#M75138</link>
      <description>&lt;P&gt;Similar to what others have mentioned, I would suggest:&lt;/P&gt;&lt;P&gt;1. Make sure you have opened Pro on the machine the script is running on, and configure licensing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Make sure your script is using the Python that comes with Pro, rather than the one that comes with ArcMap.&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. If you haven't already rewritten the script to work with how Pro publishes services, you'll need to do that - it's completely different.&amp;nbsp; I had to go through this a few years ago, and here's some code from my script that republishes a service.&amp;nbsp; NOTE: this is used for a standalone, non-federated ArcGIS Server. So ArcGIS Portal is not involved. I don't know if this code represents the best or most elegant way of publishing a service, but it's what I managed to piece together, and it works for me. It's a function that the script calls at some point. It also produces a message that is part of the email that the script sends upon finishing.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def create_sd(path_aprx, aprx_map_name, project_dir, conn_string, folder, feature_count, overwrite=False):
    """ Create an sd file from an aprx. If feature_service is True, the
        sd is configured to be published as a feature service on AGOL."""
    # Overwrite output for GP tools
    arcpy.env.overwriteOutput = True
    # Check that input file is an .aprx
    if path_aprx.endswith(".aprx"):
        # Reference map to publish
        aprx = arcpy.mp.ArcGISProject(path_aprx)
        m = aprx.listMaps(aprx_map_name)[0]
        # Get service draft name from the input .mxd file name
        service_def_draft_name = os.path.basename(path_aprx).replace(".aprx", ".sddraft")
        # Get service name
        service_name = "".join(service_def_draft_name.split(".")[0])
        # Path to service draft definition file
        path_service_draft_def = os.path.join(project_dir, service_def_draft_name)
        # Create MapServiceDraft and set metadata and server folder properties
        sddraft = arcpy.sharing.CreateSharingDraft("STANDALONE_SERVER", "MAP_SERVICE", service_name, m)
        sddraft.targetServer = conn_string
        sddraft.summary = "Write your item summary here"
        sddraft.credits = "Your organization"
        sddraft.description = "Description of your item goes here."
        sddraft.serverFolder = folder
        sddraft.tags = "your tags"
        sddraft.overwriteExistingService = True
        # Create Service Definition Draft file
        sddraft.exportToSDDraft(path_service_draft_def)
        # Create the name for the service definition
        service_def_name = "{}withtable.sd".format(service_name)
        # Path to the service definition on disk
        path_service_def = os.path.join(project_dir, service_def_name)
        # Stage the service definition file
        arcpy.StageService_server(path_service_draft_def, path_service_def)
        try:
            # Upload the service definition to publish the service to ArcGIS Server
            arcpy.UploadServiceDefinition_server(path_service_def, conn_string)
            # Return true if service published
            return True
        except:
            # Create message if publish service fails. Message used in email sent by script.
            sd_mess = "*** Unable to Publish Service. Service needs to be manually restarted. ***"
            # Return message
            return sd_mess
    else:
        # Create message if input file is not .aprx. Message is used in email sent by the script.
        sd_mess = "Input Path Must Point to an .aprx"
        # Return message
        return sd_mess&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Mar 2026 18:08:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/overwrite-a-map-service-using-a-python-script/m-p/1688722#M75138</guid>
      <dc:creator>AllenDailey1</dc:creator>
      <dc:date>2026-03-05T18:08:01Z</dc:date>
    </item>
  </channel>
</rss>

