In my org we have issues of not having enough Arc licenses. I would like to be able to trap this, rather than have the python script blow up on me. My script bombs right at the import arcpy line when all licenses are in use. When there are licenses available, this works likes a charm.
here is the beginning of my script:
import os
import sys
import shutil
import arcpy
pathName = str(sys.argv[1])
print pathName
Here is the response:
Traceback (most recent call last):
File "C:\Dropbox\Work\EclipseSrc\src\arcMapScripts\dluProc\arcPyNad27toNad83_Auto.py", line 4, in <module>
import arcpy
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 17, in <module>
from geoprocessing import gp
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\__init__.py", line 14, in <module>
from _base import *
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 568, in <module>
env = GPEnvironments(gp)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 565, in GPEnvironments
return GPEnvironment(geoprocessor)
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 521, in __init__
self._refresh()
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 523, in _refresh
envset = (set(env for env in self._gp.listEnvironments()))
RuntimeError: NotInitialized
You can use the CheckProduct function to check if licences are available. See this example copy/pasted from the Help page:
import sys
import arcpy
arcpy.env.workspace = "c:/data/world.gdb"
if arcpy.CheckProduct("ArcInfo") == "Available":
arcpy.PolygonToLine_management("Lakes", "LakeLines")
else:
msg = 'ArcGIS for Desktop Advanced license not available'
print(msg)
sys.exit(msg)