Failed to import ArcPy when there is no license

2196
3
05-28-2018 02:10 AM
Prem_KumarThogiti
New Contributor

Hi Team,

Here is the situation I am in. I am writing a geoprocessing service in python using Arcpy module, first thing I do is to import the arcpy module using "import arcpy" on desktop machine with PyCharm where ArcGIS Desktop 10.5 (v 10.5.0.6491) is installed.

It works perfectly fine most of the times but if there are no ArcMap desktop licenses to checkout, the first line of import statement fails with "RuntimeError: NotInitialized".

I know we can use arcpy.CheckProduct to check the availability of extensions and products but import arcpy module itself fails in the first place. 

Could you please anyone suggest what is the best choice to handle such scenario to check whether we can import arcpy else return with appropriate message to exit gracefully.

Thanks in advance.

Regards,

Prem

3 Replies
DanPatterson_Retired
MVP Emeritus

several ways, but it depends on your script.

You could put in a  try, except block

import sys
try:
    import arcpy
except:
    print("no arcpy license")
finally:
    sys.exit()
Prem_KumarThogiti
New Contributor

Hi Dan,

I appreciate your time responding to my question. Thank you. That's exactly what I wrote and moving ahead but I thought there must be a pythonic way to check whether ArcMap license is available on the machine or not. 

The other day I was trying this.

import imp

info=imp.find_module('arcpy')

mod = imp.load_module('arcpy', *info)

Even at the line#3, I hit the same. Of course, line#2 at least tells me whether ArcMap is installed or not.

However, I can go ahead with the exception indicator. Thanks Dan.

0 Kudos
XanderBakker
Esri Esteemed Contributor

If your objective is to create a geoprocessing service, you will not need to check for the license since it will be present in ArcGIS Server. Just make sure you have a license available the moment you run the script before publishing the GP service to the Server, which you will always have, since you will have to run it from inside Desktop in order to be able to publish the script as a service.