Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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()
share|improve this question
1  
Can you perhaps post your code so that we can see where you are placing the import statement? –  PolyGeo Feb 1 '13 at 7:59
    
Imports work fine for me. What is the stats module? If it is this, it will not work on Python 2.7: pypi.python.org/pypi/stats/0.1.2a –  blah238 Feb 1 '13 at 13:02
    
No, I am not using that and instead its a custom made library for testing with Add-In. Have you tried importing that from within python Add-In? –  Ibe Feb 1 '13 at 22:39
    
Importing what? –  blah238 Feb 2 '13 at 2:21
    
a bit of confusion: So you simply imported above stats library not with Python 2.7, right? My question is tied to python Add-In (Python 2.7). –  Ibe Feb 2 '13 at 2:26

1 Answer 1

up vote 6 down vote accepted

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:

Screenshot

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.

share|improve this answer
    
Thanks. It proved helpful in testing and correction. –  Ibe Feb 5 '13 at 10:02
    
Ditto, very helpful succinct info for testing/correction at 10.2.x, thank you for posting. Files were being copied to the AppData\Local\ESRI\Desktop10.2\AssemblyCache directory -- Wayne –  T. Wayne Whitley May 29 at 20:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.