Accessing licenses and extensions in Python

Whenever a tool is executed in a script, an ArcGIS Desktop license is required. Tools from ArcGIS extensions, such as ArcGIS Spatial Analyst, require an additional license for that extension. If the necessary licenses are not available, a tool fails and returns error messages. For example, if you install with an ArcView license, and you attempt to execute a tool set for an ArcEditor or ArcInfo license, the tool will fail.

When using an ArcView or ArcEditor license, a script should set the product to ArcView or ArcEditor. Likewise, when using an Engine or EngineGeoDB license, a script should set the product to Engine or EngineGeoDB. If a license is not explicitly set, the license will be initialized based on the highest available license level the first time an ArcPy tool, function, or class is accessed.

Every tool checks to ensure it has the appropriate license. If it doesn't have what's required, it fails. To guard against the situation of executing part way through and failing, you can perform a check at the top of your script and fail immediately.

TipTip:

The setting of the product and extensions is only necessary within stand-alone scripts. If you are running tools from the Python window or using script tools, the product is already set from within the application, and the active extensions are based on the Extensions dialog box.

Desktop, Engine/Server licenses

Product modules are imported prior to the import of arcpy to define the desktop license used by a script. The CheckProduct function can be used to the check the availability of desktop licenses, while the ProductInfo function reports what the current product license is.

LegacyLegacy:

The product level should be set by importing the appropriate product module (arcinfo, arceditor, arcview, arcserver, arcenginegeodb, arcengine) prior to importing arcpy. The SetProduct function is a legacy function and cannot set the product once arcpy has been imported.

Extension licenses

Licenses for extensions can be retrieved from a license manager and returned once they are no longer needed. CheckExtension is used to see if a license is available to be checked out for a specific type of extension, while CheckOutExtension actually retrieves the license. Once the extension license has been retrieved by the script, extension tools can be executed. Once a script is done using tools from a particular extension, the CheckInExtension function should be used to return the license to the license manager so other applications can use it. All checked-out extension licenses and set product licenses are returned to the license manager when a script completes.

The following example executes some ArcGIS 3D Analyst tools and sets the desktop product license to ArcView, since an ArcInfo license is not required to execute tools from an extension. The script will fail if the ArcView license is not explicitly set and no ArcInfo license is available, since a desktop license is required to execute extension tools.

class LicenseError(Exception):
    pass

# Set desktop license used to ArcView
#
import arcview
import arcpy
from arcpy import env

try:
    if arcpy.CheckExtension("3D") == "Available":
        arcpy.CheckOutExtension("3D")
    else:
        # Raise a custom exception
        #
        raise LicenseError
    
    env.workspace = "D:/GrosMorne"
    arcpy.HillShade_3d("WesternBrook", "westbrook_hill", 300)
    arcpy.Aspect_3d("WesternBrook", "westbrook_aspect")

except LicenseError:
    print "3D Analyst license is unavailable"  
except:
    print arcpy.GetMessages(2)
finally:
    # Check in the 3D Analyst extension
    #
    arcpy.CheckInExtension("3D")

In the above example, the 3D Analyst extension is checked in under a finally clause, ensuring that the extension is always checked back in, whether an exception has occurred or not.

A returned value of Failed, Unavailable or NotLicensed indicates that the extension could not be successfully checked out.

Below are the extension names and their extension code names:

Extension

Extension Code

3D Analyst

3D

ArcGIS Schematics

Schematics

ArcScan

ArcScan

Business Analyst

Business

Data Interoperability

DataInteroperability

Geostatistical Analyst

GeoStats

ArcGIS Workflow Manager

JTX

Network Analyst

Network

Esri Aeronautical Solution

Aeronautical

Esri Defense Mapping

Defense

Esri Production Mapping

Foundation

ArcGIS Data Reviewer

Datareviewer

Esri Nautical Solution

Nautical

Spatial Analyst

Spatial

StreetMap

StreetMap

Tracking Analyst

Tracking

Product code names

Product Codes

ArcInfo

ArcEditor

ArcView

Engine

EngineGeoDB

ArcServer

Licensing functions

Function

Explanation

CheckExtension(extension)

Checks to see if a license is available to be checked out for a specific type of extension.

Return Value

Meaning

Available

The requested license is available to be set.

Unavailable

The requested license is unavailable to be set.

NotLicensed

The requested license is not valid.

Failed

A system failure occurred during the request.

CheckInExtension(extension)

Returns the license so other applications can use it.

Return Value

Meaning

NotInitialized

No desktop license has been set.

Failed

A system failure occurred during the request.

CheckedIn

The license has been returned successfully.

CheckOutExtension(extension)

Retrieves the license.

Return Value

Meaning

NotInitialized

No desktop license has been set.

Unavailable

The requested license is unavailable to be set.

CheckedOut

Successfully set the license.

CheckProduct(code)

Checks to see if the requested license is available.

Return Value

Meaning

AlreadyInitialized

License has already been set in the script.

Available

The requested license is available to be set.

Unavailable

The requested license is unavailable to be set.

NotLicensed

The requested license is not valid.

Failed

A system failure occurred during the request.

ProductInfo()

Returns the current product license.

Return Value

Meaning

NotInitialized

No license has been set.

ArcInfo

An ArcInfo license has been set.

ArcEditor

An ArcEditor license has been set.

ArcView

An ArcView license has been set.

ArcServer

An ArcGIS Server license has been set.

EngineGeoDB

An EngineGeoDB license has been set.

Engine

An Engine license has been set.

SetProduct(code)

Defines the desktop license.

Return Value

Meaning

CheckedOut

Successfully set the license.

AlreadyInitialized

License has already been set in the script.

NotLicensed

The requested license is not valid.

Failed

A system failure occurred during the request.


12/15/2011