Importing Python Modules in the Python addin

191
3
Jump to solution
02-20-2018 08:51 AM
JoeBorgione
MVP Esteemed Contributor

I've been working in the python addin environment for the past few months.  One of the projects I've undertaken is a series of QA QC routines that run independent of each other.  My first stab was to run each of the routines under the onClick function of one button on a toolbar.  In doing so, I'm able to import various python modules needed once, right up at the top of the script where arcpy and pythonaddins are, before any button classes are defined.

However, after working on another project for a few weeks, and returning to the QA QC project, I found the code to be difficult to read and/or maintain so I decided to define each of the processes as their own function, and simply call them from the button  onClick function.  Many of these routines / functions use modules like string, and from string, punctuation and ascii_letters.  With the the more modular approach I have found that each of 'extra' python modules need to be imported with each function.  That is, I import string a half a dozen times in each of my 6 functions that use it. 

Seems like I should be able to "share" imported python modules across my defined functions, but I must be missing something along the way.

can't wait to retire....
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

Joe...Once a module (ie string) is imported, it claims namespace and even if reimported it doesn't do anything since it is already loaded.  It is good practice to include the imports inside each of your scripts (aka, modules) since you don't know which will be used first I suspect.  So unless you want to import everything that you 'might' use during a session, just place the import statements where you need them.

View solution in original post

3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Joe...Once a module (ie string) is imported, it claims namespace and even if reimported it doesn't do anything since it is already loaded.  It is good practice to include the imports inside each of your scripts (aka, modules) since you don't know which will be used first I suspect.  So unless you want to import everything that you 'might' use during a session, just place the import statements where you need them.

View solution in original post

DanPatterson_Retired
MVP Esteemed Contributor

PS... the order of imports should be

system modules

other modules

your modules

ie

import sys      # this

import os        # and this are often imported by other modules

import numpy   # this one next since it is 'pure' except for system module imports (sys and sometimes os)

import scipy     # imports numpy all over the place

import pandas  # imports numpy etc

import Joes_Amazing_Address_Tools  # this might be a toolset that you want to import and use infrequently... you can import sys,

                                                 os etc there as well, they just won't get reimported

JoeBorgione
MVP Esteemed Contributor

Thanks Dan-  thought I may be making another rookie mistake....

can't wait to retire....
0 Kudos