AnsweredAssumed Answered

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

Question asked by emilharold on Mar 3, 2014
Latest reply on Mar 3, 2014 by emilharold
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:

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

Traceback (most recent call last):
  File "C:\E1B8\ScriptTesting\MISC\Tool\", 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?

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', ''):         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,

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()