开发者

Dynamically import a callable given the full module path?

>>> import_path('os.path.join')
<function join at 0x22d4050>

What is the simplest way to write import_path (in Python 2.6 and abov开发者_JAVA技巧e)? Assume that the last component is always a callable in a module/package.


This seems to be what you want:

def import_path(name):
    modname, _, attr = name.rpartition('.')
    if not modname:
        # name was just a single module name
        return __import__(attr)
    m = __import__(modname, fromlist=[attr])
    return getattr(m, attr)

To make it work with Python 2.5 and earlier, where __import__ doesn't take keyword arguments, you will need to use:

m = __import__(modname, {}, globals(), [attr])


Apparently the following works:

>>> p = 'os.path.join'
>>> a, b = p.rsplit('.', 1)
>>> getattr(__import__(a, fromlist=True), b)
<function join at 0x7f8799865230>


Try

def import_path(name):
  (mod,mem) = name.rsplit('.',1)
  m = __import__(mod, fromlist=[mem])
  return getattr(m, mem)

Works at least for

>>> import_path('os.walk')
<function walk at 0x7f23c24f8848>

and now

>>> import_path('os.path.join')
<function join at 0x7f7fc7728a28>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜