Saturday 21 November 2015

arcpy - Refreshing imported modules in ArcGIS Python Toolbox?


When using a python toolbox (.pyt) in ArcMap, I'll typically follow a pattern where the .pyt file itself is simply a wrapper to collect input arguments and define the tools themselves. Supporting code is kept in separate unit-testable modules.


Example:


import supporting_module


class MyTool(object):

...

def execute(self, parameters, messages):
"""The source code of the tool."""
some_input = parameters[0].valueAsText
some_output = parameters[1].valueAsText


supporting_module.do_something(some_input, some_output)
arcpy.SetParameter(2, some_output)

This works out really well but I've run into one frustrating issue during development. Right-click -> Refresh on the .pyt in ArcMap only refreshes the .pyt file's code. It does not refresh the imported modules, so I have to close and re-open ArcMap whenever I change something there. Fortunately, since I'm testing the code independently, I don't have to do this a ton, but it's still a major hassle. Is there any way around this? Somewhat related - is there any way to fully refresh the Python console (i have a custom site-package that I must also close/reopen ArcMap to pull in changes from as well)?


I'm using ArcMap 10.2.1.



Answer



I found this possibility, https://stackoverflow.com/questions/1517038/python-refresh-reload


The one caveat is that if you have any variables assigned to the module, they will need to be assigned again.


But as you have it written above, you could do this:


import supporting_module

def execute()
reload(supporting_module)
...

This way every time you run the tool you'll be sure to have the updated module. Once development is done, this can be taken out.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...