9

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Roland
  • 1,290
  • 9
  • 21

3 Answers3

8

@KHibma and others made me wonder if I might get the information by using arcpy to check the version. I dug around somewhat blindly and I think I've got something even if it's not what I was expecting. I haven't checked under AGS yet.

It produces a nice dict:

>>> d=arcpy.GetInstallInfo()
>>> d

{'BuildNumber': u'2414',
 'InstallDate': u'5/16/2012',
 'InstallDir': u'C:\\ArcGIS\\Desktop10.0\\',
 'InstallTime': u'16:32:14',
 'InstallType': u'N/A',
 'Installer': u'abock',
 'ProductName': u'desktop',
 'SPBuild': u'10.0.4.4000',
 'SPNumber': u'4',
 'SourceDir': u'\\\\igskahhwvsawesm\\arcgis_10\\Desktop\\Desktop\\',
 'Version': u'10.0'}

So I can say:

if d['ProductName'] == u'desktop':
    ...
else:
    ....

Thanks for kicking that around!

Roland
  • 1,290
  • 9
  • 21
  • Just noticed this comes back as "Desktop" in 10.1, so better locase that if statement to use d['ProductName'].lower(). – Roland Jun 04 '13 at 20:13
  • Can you post what the result is on Server? Could be useful for future reference. – Petr Krebs Jun 04 '13 at 20:18
  • Definitely. Sorry for the delay. Had to go through my colleague who runs the AGS installation. Under 10.1, he got "Server". We don't have a 10.0 AGS instance, but running the result through some kind of capitalization function is probably a good idea. – Roland Jun 04 '13 at 21:15
  • I forgot about arcpy.GetInstallInfo(). While it's purpose isn't to tell you what you're running under (it acts much the same as ListInstallations ... I believe it only returns engine|desktop when run from 32 and server when run from 64bit Python. But if you're back at 10.0 like it shows in your message, you'll have both server and desktop to decide upon – KHibma Jun 04 '13 at 21:22
  • KHibma: so you're implying that when running in Desktop within the 64-bit background geoprocessing worker process, it would return "Server" as well? – Petr Krebs Jun 04 '13 at 21:25
  • @KHibma - I wasn't clear on if you're back at 10.0 like it shows in your message, you'll have both server and desktop to decide upon in your post. FWIW, I have both 10.0 and 10.1 to support, been sticking w/32-bit Python. (would also be interested in your response to Petr's question). – Roland Jun 04 '13 at 21:37
  • @PetrKrebs no, when run from 64bit Python against 64bit BG (no AGS installed), it returns Desktop. – KHibma Jun 04 '13 at 21:41
  • @Roland ugh. Having to support different versions is problematic, if it was only the 10.1, using the different "bits" would have helped you through this by allowing you to make assumptions. (also assuming 64bit BG was NOT installed with 10.1) – KHibma Jun 04 '13 at 21:42
  • @KHibma so, could I ever get the response "Server" when I should not? Or is it the case that if I have both Desktop and Server installed on the same machine (like your answer shows), would GetInstallInfo() yield a list of 2 dicts? – Roland Jun 04 '13 at 22:03
  • 2
    @Roland so I tested a couple more things after talking to a colleague. I dont have 10.0 with DT/AGS on the same machien, so I dont know how this will go, but try "arcpy.ProductInfo()". It tells you ArcINFO or ArcSERVER ... this actually might do it for you IF you're either running on DT, or the script as a GP Service in BOTH 10.0 and 10.1 – KHibma Jun 04 '13 at 22:10
  • Interesting. Talking w/Curtis. We thought that referred to license level. We were looking at arcpy__file__. Checking Server... – Roland Jun 04 '13 at 22:13
  • +1 for @KHibma's last comment, which seems to be the answer. Didn't want to mark his original answer below as "the answer". See Curtis' post for lots of gory detail that seems to show what I'm looking for (we haven't tested under arcpy-64 bit/Background). – Roland Jun 04 '13 at 23:25
5

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 
Curtis Price
  • 1,560
  • 13
  • 12
  • 1
    So it seems like arcpy.file, arcpy.GetInstallInfo(), and arcpy.ProductInfo() will all yield similar information (even if one produces "Desktop" while the other produces "ArcInfo"). I'm thinking the arcpy.ProductInfo() option is the most concise and readable option. – Roland Jun 04 '13 at 23:20
  • Haven't tested it but productinfo may give you the highest available license level if you haven't specifically imported it with "import arceditor" for example). Meaning if you only have ArcEditor available, that's what you'll probably get back. Funny that Server doesn't do the same thing, since it also supports multiple license levels (basic, standard, enterprise) – Curtis Price Jun 05 '13 at 00:14
  • Should be noted that this Python code assumes the software is installed using the normal folder naming conventions. It is pretty rare for someone not to use the default folder name "Desktop10.1" evn if they may move the path around (for example, we install to "C:\ArcGIS\Desktop10.1". – Curtis Price Aug 09 '14 at 19:28
1
arcpy.ListInstallations()
[u'desktop', u'engine', u'server']

So you can do a check of whats installed. This however wont get you very far if both DT and Server are installed on the same machine. As for a function that tells you what you're running under, no, doesnt exist.

Thinking out loud: DT is 32bit, Server is 64bit. You could do a sys.path and inspect which version of Python is being used.

or

If you know its either running as a GP Service (not just using server's python) vs DT, you could inspect the path of arcpy.env.scratchFolder. On a typical install it'll always look something like C:\arcgisserver\directories\arcgisjobs\[service]_gpserver\[guid]\....

KHibma
  • 16,786
  • 1
  • 31
  • 55