AnsweredAssumed Answered

Calling several scripts into another python script

Question asked by simpson.ben1990 on Nov 13, 2015

Hi

 

I am trying to update a python addin for ArcGIS, which is a toolbar with four buttons. Mostly what the buttons do is transfer data from multiple personal geodatabases to a single oracle database. I have one script for each button and I want to call these four scripts in a single script.

 

This is the script which I am trying to call the other four scripts into. The issue that I am having is that the from package import * is causing the four buttons to fall over (they appear as a red circle with a line through it and the word missing next to it). I have tried keeping all four scripts within a single script but I would prefer to keep them separate. Everything I try and do seems to cause the buttons to fall over, even if it is just adding a print statement.

 

I have tried putting print dir() after importing package. The classes from the four scripts are being imported fine as far as I can tell.

 

Can anyone see a reason why this script is causing the buttons in the toolbar to fall over?

 

I have included the __init__.py script at the bottom which I copied from a similar question (http://stackoverflow.com/questions/5134893/importing-python-classes-from-different-files-in-a-subdirectory). The __init__.py and the four scripts (Append_Models_Oracle.pv, SAGIS_to_Oracle.py, Import_GIS1_csv.py and Import_Determinands_csv.pv) are all located within the package folder.

 

import arcpy
import pythonaddins
import os
import sys
import getopt
import fnmatch
import traceback
import pyodbc
import csv
from package import *
from datetime import datetime
import logging
class AppendModelsOracle(object):
    """Implementation for Append_Models_Oracle.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        package.Append_Models_Oracle.AppendModelsOracle()
class SAGIStoOracle(object):
    """Implementation for SAGIS_to_Oracle.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        package.SAGIS_to_Oracle.SAGIStoOracle()
class ImportGIS1CSV(object):
    """Implementation for Import_GIS1_CSV.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        package.Import_GIS1_csv.ImportGIS1CSV()
class ImportDeterminandCSV(object):
    """Implementation for Import_determinand_csv.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        package.Import_Determinands_csv.ImportDeterminandCSV()

 

__init__.py

def _import_package_files():
    """ Dynamically import all the public attributes of the python modules
        in this file's directory (the package directory) and return a list
        of their names.
    """
    import os
    exports = []
    globals_ = globals()
    locals_ = locals()
    package_path = os.path.dirname(__file__)
    package_name = os.path.basename(package_path)
    for filename in os.listdir(package_path):
        modulename, ext = os.path.splitext(filename)
        if modulename[0] != '_' and ext in ('.py', '.pyw'):
            # create a package relative subpackage name
            subpackage = '{}.{}'.format(package_name, modulename)
            module = __import__(subpackage, globals_, locals_, [modulename])
            modict = module.__dict__
            names = (modict['__all__'] if '__all__' in modict else [name for name in modict if name[0] != '_'])  # public names
            exports.extend(names)
            globals_.update((name, modict[name]) for name in names)
    return exports
if __name__ != '__main__':
    __all__ = ['__all__'] + _import_package_files()  # '__all__' in __all__

 

Thanks in advance for any help,

 

Ben.

Outcomes