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

2266
2
06-19-2012 12:31 PM
MichaelMarkieta
New Contributor III
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
Tags (2)
2 Replies
JasonScheirer
Occasional Contributor III
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.
0 Kudos
MichaelMarkieta
New Contributor III
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 *
0 Kudos