1.from importlib import import_moduledef load_object ( path ):"""Load an object given its absolute object path, and return it.
object can be a class, function, variable o instance.path ie: 'scrapy.contrib.downloadermiddelware.redirect.RedirectMiddleware'"""
try :dot = path . rindex ( '.' )except ValueError :raise ValueError ( "Error loading object ' %s ': not a full path" % path )
module , name = path [: dot ], path [ dot + 1 :]try :mod = import_module ( module )except ImportError as e :raise ImportError ( "Error loading object ' %s ': %s " % ( path , e ))
try :obj = getattr ( mod , name )except AttributeError :raise NameError ( "Module ' %s ' doesn't define any object named ' %s '" % ( module , name ))
return obj2.RedirectMiddleware=eval("scrapy.contrib.downloadermiddelware.redirect.RedirectMiddleware")3.导入模块:module = __import__("sys")module.version'2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]'导入函数:import stringsplit = getattr(string, 'split')<function split at 0x00CDBCB0>split("hello, world", ",")from string import splitsplit= globals()['split']<function split at 0x00CDBCB0>
项目环境配置settings.py配置的字符串转化为Object
最新推荐文章于 2020-10-22 09:09:33 发布