[GIS] Run Python child script using Python Add-In button class

arcgis-10.1arcpypython-2.7python-addin

I have a Python script which I want to execute using Python Add-In button class. Right now, I am using following:

class child(object):
    """Implementation for Toolbar_addin.button2 (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        os.system('C:/temp/child.py')

but it returns nothing when I click Python Add-In button as part of sub-menu. Is it the right way to fire child scripts using button class?

Note: child script works perfectly as a stand alone script

Best Answer

@NathanW's suggestion works for me and is what I would suggest doing as well.

I have in the Install folder within my Python Add-in directory:

  • child.py:

    import os, datetime
    
    def writeDummyFile(path):
        with open(path, "w") as f:
            f.write(datetime.datetime.now().ctime())
    
    if __name__ == "__main__":
        writeDummyFile(r"C:\temp\test.txt")
        with open(r"C:\temp\test.txt", "r") as f:
            print f.readline()
    

    (when run standalone prints the current time as read from the just-written file)

  • TestChildScriptAddin.py:

    import arcpy
    import pythonaddins
    import sys
    import os
    
    sys.path.append(os.path.dirname(__file__))
    import child
    
    class LaunchChildScriptButton(object):
        """Implementation for LaunchChildScript.button (Button)"""
        def __init__(self):
            self.enabled = True
            self.checked = False
        def onClick(self):
            child.writeDummyFile(r"C:\temp\test.txt")
    

    (when clicked, the current time is written to the file)

This works fine for me at 10.1 SP1. Have you tried doing something similar (barebones test)?

Related Question