Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

share|improve this question
2  
Why not import the python module and just call methods on it? –  Nathan W May 20 '13 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 '13 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 '13 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 '13 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 '13 at 11:12

2 Answers 2

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

share|improve this answer
    
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 '13 at 4:26
    
Post a complete code sample that reproduces the problem otherwise we are getting nowhere. –  blah238 May 21 '13 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 '13 at 16:05

you may want to change

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

to

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

this should run the script.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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