Django-importlib模块使用

in stark.py

	class DownFile(object):
		def __init__(self,name)
			self.name = name
		def getName(self):
			return self.name
in views.py

	import importlib
	def func():
		'''
			importlib模块
			以字符串的形式导入模块
		'''
		DB_PATH = 'app01.stark.DownFile'

		module_path,cls_name = DB_PATH.rsplit('.',maxsplit=1)
		module_obj = importlib.import_module(module_path)

		# 去模块中导入类
		cls = getattr(module_obj,cls_name)

		# 类实例化
		obj = cls("路飞")
		print(obj.getName())


你可能感兴趣的:(Django)