Importing everything ( * ) dynamically from a modu

2020-02-26 03:51发布

I have a Python module that I want to dynamically import given only a string of the module name. Normally I use importlib or __import__ and this works quite well given that I know which objects I want to import from the module, but is there a way to do the equivalent of import * dynamically. Or is there a better approach?

I know in general its bad practice to use import * but the modules I'm trying to import are automatically generated on the fly and I have no way of knowing the exact module which contains the class I'm addressing.

Thanks.

3条回答
姐就是有狂的资本
2楼-- · 2020-02-26 03:55

The following is highly sinful and will condemn you to purgatory or worse

# module A
myvar = "hello"

# module B
import inspect
def dyn_import_all(modpath):
  """Incredibly hackish way to load into caller's global namespace"""
  exec('from ' + modpath + ' import *', inspect.stack()[1][0].f_globals)

# module C
from module B import dyn_import_all
def print_from(modpath):
  dyn_import_all(modpath)
  print(myvar)
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2020-02-26 04:08

I came up with some ugly hacky code, it works in python 2.6. I'm not sure if this is the smartest thing to do though, perhaps some other people here have some insight:

test = __import__('os',globals(),locals())
for k in dir(test):
    globals()[k] = test.__dict__[k]

You probably want to put a check here to make sure you aren't overwriting anything in the global namespace. You could probably avoid the globals part and just look through each dynamically imported module for your class of interest. This would probably be much better than polluting the global namespace with everything you are importing.

For example, say your class is named Request from urllib2

test = __import__('urllib2',globals(),locals())
cls = None
if 'Request' in dir(test):
    cls = test.__dict__['Request']
    # you found the class now you can use it!
    cls('http://test.com')
查看更多
三岁会撩人
4楼-- · 2020-02-26 04:18

Use update for dicts:

globals().update(importlib.import_module('some.package').__dict__)

Note, that using a_module.__dict__ is not the same as from a_module import *, because all names are "imported", not only those from __all__ or not starting with _.

查看更多
登录 后发表回答