Monday, 12 November 2018

can arcpy detect whether it's running in Desktop or Server?


I'm developing some code that I want to run both as an ArcGIS Server web service and as a standalone script. I want to be able to modify the execution slightly depending on whether it's running as an AGS web service or as a standalone python/within Desktop.


Is there a way to detect this?


I've done some searching but haven't found anything very satisfactory. I have seen arcpy.mapping.CreateGISServerConnectionFile, but this sets connections to a server as opposed to checking the current environment. I've also looked for information on how services are run w/in AGS (e.g., this), but I'm not really seeing anything along the lines of an arcpy.isWebService() type of function. Do I just need to check the environment (like to see if "%scratchFolder% has been set or examining something related to sys.argv)? Seems kind of an unstable solution.



Answer




I suggested this, after looking over the results of sys.executable, arcpy.GetInstallInfo(), ListInstallations(), and ProductInfo(), none of which gave me good answer as to which arcpy I was running. @KHibma, something for the arcpy team to think about - it would be a nice property (arcpy.version -- like sys.version) to have.


>>> import arcpy
>>> arcpy.__file__
'C:\\ArcGIS\\Desktop10.1\\arcpy\\arcpy\\__init__.py'

I cast this test as a function. If you need to know the Python bits, I think it's best to just test sys.version for containing "32 bit" so I didn't include that. Digging out the ArcGIS version is a bit more code so I went ahead and included that in the function.


# report arcpy platform and version
# author: Curtis Price, cprice@usgs.gov
# date: 2014/08/09 (update to support ArcGIS Pro)


def ArcPyVersion():
"""Report ArcPy version

example

>>> ArcPyVersion()
'Desktop 10.1.3143'
"""
import arcpy
f = arcpy.__file__

if f.find("Desktop") > 0:
v = "Desktop"
elif f.lower().find("arcgis pro") > 0:
v = "ArcGISPro"
else:
v = "Server"
i = arcpy.GetInstallInfo()
v = "{0} {1}.{2}".format(
v, i["Version"], i["BuildNumber"])
return v

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...