0

I'm trying to change from a.net toolbar to a Python toolbar in ArcGIS 10.2 and am having some difficulty. I have some stand alone python script which I know works and does what I need to do but I can't work out how to get the add in button to call this script or how to incorporate the script in with the button add in script.

import arcpy
import pythonaddins

class ImportCSVFile(object):
    """Implementation for Import_CSV.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        script1 = "C:\Users\xxx\Documents\AddIns\pythonAddin\Install\xxx"
        execfile(script1)

I've also tried using os.startfile(xxxxx) instead of script1 = "xxxx" and execfile(script1).

I don't get an error but the button doesn't do anything. Does anyone have any ideas of how to get this to work?

  • 2
    Could you just copy your script into the onClick() method instead of trying to call it from a location at all? Another thought... It looks like your using "xxx" as a placeholder, but make sure it is "xxx.py" – GeoJohn Aug 28 '15 at 14:13
  • Also, you could try "import os" at the top and then os.system("xxxx.py") – GeoJohn Aug 28 '15 at 14:19
  • What happens when you replace "C:\Users\xxx\Documents\AddIns\pythonAddin\Install\xxx" with r"C:\Users\xxx\Documents\AddIns\pythonAddin\Install\test.py" and do something like print "worked!" as your total code in test.py? Make sure that you have the Python window open to see any errors as you click the button. – PolyGeo Sep 1 '15 at 22:25
2

GeoJohn has the correct solution but here is the code sample

import arcpy
import pythonaddins
import os

class Run_Script(object):
    """Implementation for addin_test_addin.Run_Script (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        os.system(r'C:\Data\test\importcsv.py')

As Geojohn suggested make sure you have a .py file extension and you will be best served to have Python interpret your file path as a string literal. Do so by placing a r before the file path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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