Checking if ArcGIS extension has already been checked out manually in ArcPy?
This can be done with python, but not using arcpy as far as I know. If you use comtypes you can have some degree of access to ArcObjects. By accessing the AoInitialize class, you can check on licenses/extensions that are checked out by supplying an extension code.
Here is some sample code:
import comtypes
# load olb
esriSys = r'C:\Program Files (x86)\ArcGIS\Desktop10.3\com\esriSystem.olb'
comtypes.client.GetModule(esriSys)
import comtypes.gen.esriSystem as esriSystem
def NewObj(COMClass, COMInterface):
"""Creates a new comtypes POINTER object where\n\
MyClass is the class to be instantiated,\n\
MyInterface is the interface to be assigned"""
from comtypes.client import CreateObject
try:
ptr = CreateObject(COMClass, interface=COMInterface)
return ptr
except:
return None
# now call AoInitialize
pInit = NewObj(esriSystem.AoInitialize,
esriSystem.IAoInitialize)
# 10 is spatial analyst
licenseAvailable = pInit.IsExtensionCheckedOut(10)
if licenseAvailable:
print 'license is checked out'
else:
print 'license not checked out'
And here is an example inside ArcMap (I have taken the Snippets module and modified it a bit and saved it as a package called "arcobjects"). Also, I did not mean to make this confusing, but my variable name is misleading...It should have been called licenseCheckedOut, as that is the actual property being used, not checking for availability (which you can do in arcpy).
usage with license checked out:
And once the extension is checked back in:
This can be made a bit more "pythonic" with a context manager:
import arcview, arcpy
class Spatial(object):
'''Context manager for the ArcGIS Spatial Analyst Extension'''
def __init__(self):
self.name = 'Spatial'
from arcpy.sa import Int
try:
Int(1)
self.checkedout = True
except RuntimeError:
self.checkedout = False
def checkin(self):
if self.checkedout:
return
else:
arcpy.CheckInExtension(self.name)
def checkout(self):
if self.checkedout:
return
if arcpy.CheckExtension(self.name) == "Available":
arcpy.CheckOutExtension(self.name)
else:
raise RuntimeError("%s license isn't available" % self.name)
def __enter__(self):
self.checkout()
def __exit__(self, *args):
self.checkin()
if __name__ == '__main__':
from arcpy.sa import Int
with Spatial():
a = Int(2)
print repr(a)