2

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

9
  • 2
    Why not import the python module and just call methods on it?
    – Nathan W
    May 20, 2013 at 10:46
  • If I understand your suggestion correctly: I imported modules and generated few outputs using python Add-In. This final step will use outputs from previous steps and do some random calculations. So it has to be fired from a button.
    – Ibe
    May 20, 2013 at 10:53
  • Just put child.py somewhere you Add-in can see it. In the same folder should work and just do import child and then you can call any of the methods.
    – Nathan W
    May 20, 2013 at 10:58
  • child is in the same folder as Add-In logic script. Importing it gives an error (no such module exists -- since it is not a library). I tried following but no avail: subprocess.call(['C:/Python27/ArcGIS10.1/python.exe','C:/temp/child.py'])
    – Ibe
    May 20, 2013 at 11:09
  • 1
    You can always import any Python file, Python just needs to see where it is. Try sys.path.append("C:\\Temp") then import child
    – Nathan W
    May 20, 2013 at 11:12

3 Answers 3

3

@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)?

3
  • I have tried similar text writing under button and within child script as well before posting question -- works in both cases. It seems that the problem is occurring due to the declaration of global variables in the child script. I will comment them out to test further.
    – Ibe
    May 21, 2013 at 4:26
  • 1
    Post a complete code sample that reproduces the problem otherwise we are getting nowhere.
    – blah238
    May 21, 2013 at 5:20
  • Main error source was: saving raster data with FGDBR or GRID format. So I changed environmental settings and set scratch workspace as an intermediate folder on hard disk, instead of "Default.gdb". It solved the issue. Thanks everyone for answers.
    – Ibe
    May 23, 2013 at 16:05
-1

you may want to change

os.system('C:/temp/child.py')

to

os.startfile('C:/temp/child.py')

this should run the script.

-1

Both suggestions by Hotpepper worked for me. Perhaps you should consider:

os.system('C:\\temp\\child.py')

os.startfile('C:\\temp\\child.py')

I mean (\\) instead to (/)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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