H
Hugh Macdonald
We're starting to version a number of our python modules here, and I've
written a small function that assists with loading the versioned
modules...
A module would be called something like: myModule_1_0.py
In anything that uses it, though, we want to be able to refer to it
simply as 'myModule', with an environment variable ("MYMODULE_VERSION"
- set to "1.0") that defines the version.
I've written a module called 'moduleLoader' with the follwing function
in:
def loadModule(module, version, v = globals()):
import compiler
loadStr = "import %s_%s as %s" % (module, version.replace(".", "_"),
module)
eval(compiler.compile(loadStr, "/tmp/%s_%s_errors.txt" % (module,
version.replace(".", "_")), "single"))
v[module] = vars()[module]
The ideal situation with this would be to be able, in whatever script,
to have:
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
However, this doesn't work. The two options that do work are:
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"),
globals())
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
from moduleLoader import myModule
What I'm after is a way of moduleLoader.loadModule working back up the
scope and placing the imported module in the main global scope. Any
idea how to do this?
written a small function that assists with loading the versioned
modules...
A module would be called something like: myModule_1_0.py
In anything that uses it, though, we want to be able to refer to it
simply as 'myModule', with an environment variable ("MYMODULE_VERSION"
- set to "1.0") that defines the version.
I've written a module called 'moduleLoader' with the follwing function
in:
def loadModule(module, version, v = globals()):
import compiler
loadStr = "import %s_%s as %s" % (module, version.replace(".", "_"),
module)
eval(compiler.compile(loadStr, "/tmp/%s_%s_errors.txt" % (module,
version.replace(".", "_")), "single"))
v[module] = vars()[module]
The ideal situation with this would be to be able, in whatever script,
to have:
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
However, this doesn't work. The two options that do work are:
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"),
globals())
import moduleLoader
moduleLoader.loadModule("myModule", os.getenv("MODULE_VERSION"))
from moduleLoader import myModule
What I'm after is a way of moduleLoader.loadModule working back up the
scope and placing the imported module in the main global scope. Any
idea how to do this?