Wednesday, 15 January 2020

arcgis 10.1 - Import functions in python Add-In logic script


I am running into an error. I have a set of functions which I want to import in the python Add-In script at the time of execution, but when I built Add-In with "import stats" line then tools are unresponsive. Any suggestions whether it is possible to import functions in the Add-In script?


import arcpy
import os

import pythonaddins
import stats

optfolder = "C:/temp"

class AoI(object):
"""Implementation for rectangle_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.cursor = 5

self.shape = 'Rectangle'
os.makedirs(optfolder)

def onRectangle(self, rectangle_geometry):
"""Occurs when the rectangle is drawn and the mouse button is released.
The rectangle is a extent object."""

extent = rectangle_geometry
arcpy.Clip_management(r'C:/temp/ras', "%f %f %f %f" %(extent.XMin, extent.YMin, extent.XMax, extent.YMax),
optfolder + '/ras1', "#", "#", "NONE")

arcpy.RefreshActiveView()

Answer



If an unhandled exception, such as an ImportError, occurs before the add-in classes are instantiated they will become unresponsive, be given a [Missing] label, and have a red symbol for their icon in the case of items on toolbars or in menus.


You can confirm whether an import error is happening by wrapping your import statement with an exception handler and displaying its message in a message box, e.g.:


try:
import stats
except ImportError as e:
pythonaddins.MessageBox(e.message, "ImportError")

Screenshot



I suspect this is the case here. You need to ensure that your stats module is actually discoverable by Python. You will want to read up on how importing works if you are not sure about this:



NOTE: If you are trying to package your stats module with your add-in, e.g. by placing it in the Install folder alongside your add-in .py file(s), see this thread, which suggests adding the following line to your add-in .py file before your stats import statement:


sys.path.append(os.path.dirname(__file__))

You'll need to import the sys and os modules before that as well.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...