Select to view content in your preferred language

Can't figure out how to add a button for a python script

655
3
Jump to solution
03-03-2014 10:09 AM
Emilbrundage
Deactivated User
I'm trying to create a button that runs a python script.

I've been following the help and have reached the point where I test an add-in:
http://resources.arcgis.com/en/help/main/10.1/index.html#/Testing_an_add_in/014p00000026000000/

I run the script makeaddin.py as stated in step 1 and get this error:

Traceback (most recent call last):
  File "C:\E1B8\ScriptTesting\MISC\Tool\makeaddin.py", line 5, in <module>
    current_path = os.path.dirname(os.path.abspath(__file__))
NameError: name '__file__' is not defined

Should __file__ been replaced with a path, and somehow that didn't happen for me? If so, can I add it manually just to make things easy on myself?

makeaddin.py:

import os import re import zipfile  current_path = os.path.dirname(os.path.abspath(__file__))  out_zip_name = os.path.join(current_path,                              os.path.basename(current_path) + ".esriaddin")  BACKUP_FILE_PATTERN = re.compile(".*_addin_[0-9]+[.]py$", re.IGNORECASE)  def looks_like_a_backup(filename):     return bool(BACKUP_FILE_PATTERN.match(filename))  with zipfile.ZipFile(out_zip_name, 'w', zipfile.ZIP_DEFLATED) as zip_file:     for filename in ('config.xml', 'README.txt', 'makeaddin.py'):         zip_file.write(os.path.join(current_path, filename), filename)     dirs_to_add = ['Images', 'Install']     for directory in dirs_to_add:         for (path, dirs, files) in os.walk(os.path.join(current_path,                                                         directory)):             archive_path = os.path.relpath(path, current_path)             found_file = False             for file in (f for f in files if not looks_like_a_backup(f)):                 archive_file = os.path.join(archive_path, file)                 print archive_file                 zip_file.write(os.path.join(path, file), archive_file)                 found_file = True             if not found_file:                 zip_file.writestr(os.path.join(archive_path,                                                'placeholder.txt'),                                   "(Empty directory)")


The script I'm trying to turn into a button (at this stage, Tool_addin.py):

import arcpy import pythonaddins   def selecttoDQ():     #do things##  ####################  class ListLayers(object):     """Implementation for Tool_addin.btn1 (Button)"""     def __init__(self):         self.enabled = True         self.checked = False     def onClick(self):         selecttoDQ()



Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
You don't need to make any edits to the makeaddin.py at all. Usually you only get that error if it is being run through an interpreter. Do you have some strange file associations with .py files by any chance?

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Honored Contributor
You don't need to make any edits to the makeaddin.py at all. Usually you only get that error if it is being run through an interpreter. Do you have some strange file associations with .py files by any chance?
0 Kudos
Emilbrundage
Deactivated User
Ah okay. Initially my button wasn't showing up so I ran the script in IDLE, got the error, and assumed that was the issue. Now I've done the double-click run and followed the rest of the steps. This time the button is there but it doesn't do anything. I will keep poking away and hopefully get it working. Thanks for the response.
0 Kudos
Emilbrundage
Deactivated User
It looks like the issue is with the script. I tried the example script (zoom to selection) and it worked fine. Is there ever a case where scripts need to be altered to function as a button? My script has no parameters if that matters...

EDIT: Scripting error, problem soloved 🙂
0 Kudos