[转]Python动态导入模块
2013-01-14 16:50
495 查看
原文地址:http://blog.csdn.net/huzhenwei/article/details/5393992
一 动态导入模块
Python的import不能接受变量,所以应该用 __import__函数来动态导入。
二 检查模块是否安装
使用__import__函数也可以用来检查模块是否已安装,略微修改上面的代码即可。
使用imp.find_module()来检查不方便,如find_module('zope.interface')会抛出异常——因为这个函数无法查找子模块。
模块加载后,就可以在sys.module这个字典里找到加载的模块名了。
__import__
except in cases where you want to import a module whose name is only known at runtime.
一 动态导入模块
Python的import不能接受变量,所以应该用 __import__函数来动态导入。
modules = ['OpenSSL', 'math', 'Crypto', 'MySQLdb', 'sqlite3', 'zope.interface', 'pyasn1', 'twisted', 'django'] for each in modules: try: __import__(each) except Exception as e: print(e)
二 检查模块是否安装
使用__import__函数也可以用来检查模块是否已安装,略微修改上面的代码即可。
使用imp.find_module()来检查不方便,如find_module('zope.interface')会抛出异常——因为这个函数无法查找子模块。
模块加载后,就可以在sys.module这个字典里找到加载的模块名了。
__import__
except in cases where you want to import a module whose name is only known at runtime.
相关文章推荐
- python 动态导入模块、断言
- python 动态导入模块
- 在python中实现动态导入模块importlib.import_module
- python中动态导入模块
- python中动态导入模块
- 基础入门_Python-模块和包.运维开发中__import__动态导入最佳实践?
- 在python中实现动态导入模块importlib.import_module
- Python动态导入模块
- Python动态导入模块的方法实例分析
- Python动态导入模块
- Python中动态导入模块-pybatis的准备阶段(七)
- python动态导入模块、判断模块是否已安装
- python动态导入模块、检查模块是否安装
- python核心编程学习笔记-2016-08-02-02-模块动态导入
- python动态导入模块、检查模块是否安装
- python3 动态模块导入与全局变量使用
- Python 实现抽象类的两种方式+邮件提醒+动态导入模块+反射(参考Django中间件源码)
- python动态导入模块
- python动态导入模块
- python动态加载模块