The OpenERP python code development cycle is to edit your code, restart the server and test it.
Restarting the server is necessary, because it’s what makes your source code to be reloaded into memory, but it adds an annoying delay in your work pace.
Since python is such a dynamic language, I wonder if there is a way to force a running python interpreter (the app server ) to reload on the fly a code module, so that it can be tested without restarting the app server?
Update:
Following down the reload path suggested by @ecatmur, I got the the code below, but it still doesnt work:
class module(osv.osv):
_inherit = "ir.module.module"
def action_reload(self, cr, uid, ids, context=None):
for obj in self.browse(cr, uid, ids, context=context):
modulename = 'openerp.addons.' + obj.name
tmp = __import__(modulename)
pycfile = tmp.__file__
modulepath = string.replace(pycfile, ".pyc", ".py")
code=open(modulepath, 'rU').read()
compile(code, modulename, "exec")
execfile(modulepath)
reload( sys.modules[modulename] )
openerp.modules.registry.RegistryManager.delete(cr.dbname)
openerp.modules.registry.RegistryManager.new(cr.dbname)
UPDATE: since v8 the Odoo server provides an
--auto-reloadoption to perform this.Excellent question, I’ve often wondered the same. I think the main problem with the code you posted is that it only reloads the OpenERP module’s
__init__.pyfile, not all the individual files. Thereimportmodule recommended by ecatmur takes care of that, and I also had to unregister the module’s report parsers and model classes before reloading everything.I’ve posted my
module_reloadmodule on Launchpad. It seems to work for changes to model classes,osv_memorywizards, and report parsers. It does not work for old-style wizards, and there may be other scenarios that don’t work.Here’s the method that reloads the module.