2 Replies Latest reply on Jun 19, 2012 1:25 PM by markieta

    Separating business logic and add-in script [Python Add-In]

    markieta
      Hi all,

      I was hoping someone could fill me in on how I can properly seperate my python addins' business logic and the main add-in script.

      The config.xml file lists:
      <AddIn language="PYTHON" library="PythonAddIn_addin.py" namespace="PythonAddIn_addin">

      in its top most level. My library will contain more than just the default PythonAddIn_addin.py file.

      Ideally, I would like to be able to:
      from Install.businesslogic import Analytics

      to import my business logic function definition Analytics().

      Currently, this breaks my application, while keeping all the needed code within the PythonAddIn_addin.py file works.
      The moment I try to import code from a relative path (within the Install folder), the add-in breaks.

      Ideas? I couldn't live with all my business logic sitting inside the PythonAddIn_addin.py file... its getting huge.

      Cheers,

      Michael
        • Re: Separating business logic and add-in script [Python Add-In]
          JScheirer-esristaff
          Rather than Install.businesslogic, does import businesslogic work? It should. If not, try sys.path.append(os.path.dirname(__file__)) before your import thought I thought I made relative imports work.
          • Re: Separating business logic and add-in script [Python Add-In]
            markieta
            Rather than Install.businesslogic, does import businesslogic work? It should. If not, try sys.path.append(os.path.dirname(__file__)) before your import thought I thought I made relative imports work.


            I was about to update my question with that exact answer!

            For those who come across the same problem. You can seperate your code into bite sized chunks and import them as modules as follows:

            # Import os, sys; append relative directory to path
            import os
            import sys
            sys.path.append(os.path.dirname(__file__))
            import arcpy
            import pythonaddins
            
            # Import your modules and add functions to namespace
            from businesslogic import *