Resolver Hacks contains information and code for doing fun and useful things with Resolver One. This site is created and maintained by Michael Foord, not by Resolver Systems. Please read the disclaimer before using any of the code on this site.
Import with Reload
When you are creating functions and classes to be used inside your spreadsheets, it often makes sense to develop these in Python files and import them into your spreadsheet.
So long as you have saved the current spreadsheet, you can import Python modules that are saved in the same directory as your spreadsheet. The problem is, when Python imports modules it caches them. That means that if you edit your code, the new version won't be used when the spreadsheet is recalculated. There is a reload function, but this only reloads the module. I usually use the from module import name form, and reload doesn't update names imported like this.
The following code can be used instead of an import, whilst you are working on your modules. It reloads the modules and returns the names you want every time it is executed, so your latest changes will always get picked up.
Warning
Saving your spreadsheet in a different directory will clear the previous directory form the path and remove the previously imported module.
"""
Returns specific names from a module, after reloading them.
The moduleName argument should be a string.
The names argument can either be a list of names (strings), or a single name.
"""
mod = __import__(moduleName)
components = moduleName.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
mod = reload(mod)
if isinstance(names, list):
output = []
for name in names:
output.append(getattr(mod, name))
return output
return getattr(mod, names)
You use it like this:
# Delete this line once you have finished
# developing your module
name1, name2 = reloadWithImport('module', ['name1', 'name2'])
Last edited Sat Jun 09 23:49:52 2007.

IronPython in Action