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. I have a python script that uses a mxd and can overwrite a map service that was published from ArcMap. How do I get this script to work using a Pro project instead of a mxd? I've changed the script to point to my aprx file. When I try to run the script from a command prompt, I get a 'Product License has not been initialized' error message.
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. 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.
Can you post the code? Are you using a Named User license and have opened Pro with that Named User license you are running the script on?
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).
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: https://pro.arcgis.com/en/pro-app/3.3/arcpy/sharing/featuresharingdraft-class.htm#GUID-8E27A3ED-A705...
Similar to what others have mentioned, I would suggest:
1. Make sure you have opened Pro on the machine the script is running on, and configure licensing.
2. Make sure your script is using the Python that comes with Pro, rather than the one that comes with ArcMap.
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. I had to go through this a few years ago, and here's some code from my script that republishes a service. 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.
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