用名称字符串导入模块(仅作了解即可)

# -*- coding: utf-8 -*-
#python 27
#xiaodeng
#Python学习手册 636
#用名称字符串导入模块(仅作了解即可)



#默认情况下,exec运行当前作用域中的代码。



#案例
modname='test'
result='import ' + modname
exec(result)        #运行字符串
print test          #<module 'test' from 'C:\Users\Administrator\Desktop\test.pyc'>
print test.name     #python

 

posted @ 2015-10-30 09:01  Xiao|Deng  阅读(190)  评论(0编辑  收藏  举报