+ Reply to Thread
Results 1 to 3 of 3

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

  1. #1
    Michael Markieta
    Join Date
    May 2012
    Posts
    12
    Points
    0
    Answers Provided
    1


    0

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

    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:
    Code:
    <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:
    Code:
    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

  2. #2
    Jason Scheirer

    Join Date
    Oct 2009
    Posts
    515
    Points
    491
    Answers Provided
    69


    0

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

    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.

  3. #3
    Michael Markieta
    Join Date
    May 2012
    Posts
    12
    Points
    0
    Answers Provided
    1


    0

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

    Quote Originally Posted by jscheirer View Post
    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:

    Code:
    # 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 *

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts