通过字符串形式导入模块
利用importlib模块
settings.py
class Foo(object): DEBUG = True TEMPLATES = "templates"
test.py
import importlib path = "settings.Foo" k, v = path.rsplit(".", 1) m = importlib.import_module(k) print(m) # <module 'settings' from 'D:\\PycharmProjects\\untitled\\flask_demo\\settings.py'> print(m.Foo) # <class 'settings.Foo'> foo = getattr(m, v) print(foo, type(foo), dir(foo)) # <class 'settings.Foo'> <class 'type'> ['DEBUG', 'TEMPLATES', '__class__', '__delattr__ ... for i in dir(foo): if i.isupper(): ret = getattr(foo, i) print(ret) # True templates