In Python >= 2.5, if you need to do
from . import module
but you don't know the name of module until runtime, this is how you do it:
mod = getattr(__import__('', globals(), locals(),
[name], 1), name)
(This is going on my big list of things I will change when I am the BDFL.)
no subject
Date: 2009-12-23 04:17 pm (UTC)no subject
Date: 2009-12-23 05:14 pm (UTC)__import__is a low-level hook function, designed to be overridden by libraries that need to modify the module search algorithm; its signature matches the needs of the internals, and it doesn't do all the work. The problem from a language design standpoint is there's no higher-level API to use for a dynamic import. This could be fixed with a stdlib module, but nobody seems to have bothered to write one.no subject
Date: 2009-12-24 10:47 pm (UTC)http://docs.python.org/dev/library/importlib.html
no subject
Date: 2009-12-25 02:05 am (UTC)They need to work on the dox, though. The 3.1
importlibpage make it sound like yet another bag of utilities for people writing import hooks rather than the place to look for a high level__import__wrapper. And why do I have to specify the reference point for relative imports? Doesn't that defeat the purpose?