Hello,
I programmed an initial add in in Python that works just fine. My problem is that when I make a trivial refactoring change to it, it fails to load in ArcGIS desktop.
The docs point to this being a syntax error in the code, but I checked it outside of ArcGIS and it worked just fine. I also couldn't find any information on ArcGIS (error logs, for example) about what the syntax error could be.
What can I do to find the cause of this problem? Can I find anywhere in ArcGIS a detailed error message about why this fails to load?
I post below how I refactored my code in case anyone can give me a hint about what could be wrong.
Before refactoring:
import arcpy
import pythonaddins
class Promedio(object):
"""Implementation for AddinsPyPromedio_addin.Promediobutton (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
#Compute average
After refactoring:
import arcpy
import pythonaddins
class Promedio(object):
"""Implementation for AddinsPyAverage_addin.Averagebutton (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def computeAverage(self):
#compute average
def onClick(self):
self.computeAverage()
I believe the issue is do to the fact you have nothing under the 'computeAverage' function. Try the following:
import arcpy
import pythonaddins
class Promedio(object):
"""Implementation for AddinsPyAverage_addin.Averagebutton (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def computeAverage(self):
print 'test'
def onClick(self):
self.computeAverage()
Thanks Jake for your reply.
I apologize for the confusion. The code I posted was abridged for the sake of clarity. In place of the "#compute average" comment, I actually do the computation of the average. I include this code below.
Original code: (Working)
import arcpy
import pythonaddins
class Promedio(object):
"""Implementation for AddinsPyPromedio_addin.Promediobutton (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
try:
mxd = arcpy.mapping.MapDocument("CURRENT")
layers = arcpy.mapping.ListLayers(mxd)
print "Done, Done, Done"
capaConseguida = False
layerIntersection = layers[0]
for lyr in layers:
print lyr.name
if "OutFeatures" in lyr.name:
print "IF"
capaConseguida=True
layerIntersection=lyr
if capaConseguida==True:
print "We got it"
pathLayer=""
try:
pathLayer = layerIntersection.workspacePath+"\\"+layerIntersection.name
cur = arcpy.AddField_management(pathLayer,"prommax",'FLOAT')
except:
print "Something went wrong... An error ocurred..."
print pathLayer
fields = arcpy.ListFields(pathLayer, "", "")
names=[]
for fld in fields:
if 'min' in fld.name:
names.append(fld.name)
elif fld.name=="prommax":
names.append(fld.name)
j=0
with arcpy.da.UpdateCursor(pathLayer,names) as cursor:
for row in cursor:
suma=0
for z in range(0,len(names)-1):
suma = suma + row
resultado=suma
print resultado
row[len(names)-1]=float(resultado)
cursor.updateRow(row)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
except Exception, err:
print arcpy.GetMessages(2)
print Exception, err
Below I include a trivial variation of the code above, factoring out a single line of code (line 18). This is a silly first step towards the very simple refactoring I described in my first post. This doesn't load in ArcMAP. Anyone knows why?
import arcpy
import pythonaddins
class Promedio(object):
"""Implementation for AddinsPyPromedio_addin.Promediobutton (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def calculateAverage(self):
mxd = arcpy.mapping.MapDocument("CURRENT")
return mxd
def onClick(self):
try:
mxd = self.calculateAverage()
layers = arcpy.mapping.ListLayers(mxd)
print "Done, Done, Done"
capaConseguida = False
layerIntersection = layers[0]
for lyr in layers:
print lyr.name
if "OutFeatures" in lyr.name:
print "IF"
capaConseguida=True
layerIntersection=lyr
if capaConseguida==True:
print "We got it"
pathLayer=""
try:
pathLayer = layerIntersection.workspacePath+"\\"+layerIntersection.name
cur = arcpy.AddField_management(pathLayer,"prommax",'FLOAT')
except:
print "Something went wrong... An error ocurred..."
print pathLayer
fields = arcpy.ListFields(pathLayer, "", "")
names=[]
for fld in fields:
if 'min' in fld.name:
names.append(fld.name)
elif fld.name=="prommax":
names.append(fld.name)
j=0
with arcpy.da.UpdateCursor(pathLayer,names) as cursor:
for row in cursor:
suma=0
for z in range(0,len(names)-1):
suma = suma + row
resultado=suma
print resultado
row[len(names)-1]=float(resultado)
cursor.updateRow(row)
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
except Exception, err:
print arcpy.GetMessages(2)
print Exception, err
Thanks.
You have setup to capture any error. That is good. So what are you seeing. Is the Add-in Installed, but just broken, showing no buttons or tools?\
Just to make sure, save a document with the Python window open. Then reinstall the addin - open the MXD and look at what the error says in the python window. Just a thought, not a solution to the problem.
Thanks Todd for the answer.
As you said, I can install the Add-in perfectly but what I see is a borken toolbar icon.
I tried what you said but nothing happened, still couldnt see any errors
The image below is a screenshot of what I see.
.
Thanks,
Mauricio.
1. Delete the Python add-in in ArcMap by going to Customize > Add-In Manager. Select the add-in > Delete this Add-In
2. Close ArcMap
3. Run the makeaddin.py script
4. Install the python add-in again
Your python syntax appears to be correct. I was able to create an add-in, copy/paste your code, and the add-in worked successfully.
can you also copy in the config.xml here.
When you see the the actual tool/button/* it can be a combination of things, but more than likely an issue with the config build not matching up with the python. I would just like to look over them both and make connections.
Thanks Jake, I tried that too, but nothing changed.
Of course Todd.
Here it is.
<ESRI.Configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Name>Promedio</Name><AddInID>{06aee945-65c8-4cca-acd4-8fda0343d5d8}</AddInID><Description>Promedio</Description><Version>0.1</Version><Image /><Author>Promedio</Author><Company>Promedio</Company><Date>08/05/2014</Date><Targets><Target name="Desktop" version="10.2" /></Targets><AddIn language="PYTHON" library="AddinsPyPromedio_addin.py" namespace="AddinsPyPromedio_addin"><ArcMap>
<Commands>
<Button caption="Promedio" category="Promedio" class="Promedio" id="AddinsPyPromedio_addin.Promediobutton" image="" message="Promedio" tip="Promedio"><Help heading="Promedio">Promedio</Help></Button>
</Commands>
<Extensions>
</Extensions>
<Toolbars>
<Toolbar caption="Promedio" category="Promedio" id="AddinsPyPromedio_addin.Promediotoolbar" showInitially="true"><Items><Button refID="AddinsPyPromedio_addin.Promediobutton" /></Items></Toolbar>
</Toolbars>
<Menus>
</Menus>
</ArcMap></AddIn></ESRI.Configuration>
Thanks!