Python动态加载模块的3种方法

yipeiwu_com6年前Python基础

1、使用系统函数__import_()

复制代码 代码如下:

stringmodule = __import__('string')

2、使用imp 模块

复制代码 代码如下:

import imp
stringmodule = imp.load_module('string',*imp.find_module('string'))

3、使用exec

复制代码 代码如下:

import_string = "import string as stringmodule"
exec import_string

相关文章

python3 requests库文件上传与下载实现详解

在接口测试学习过程中,遇到了利用requests库进行文件下载和上传的问题。同样,在真正的测试过程中,我们不可避免的会遇到上传和下载的测试。 文件上传: url = ztx.host...

从django的中间件直接返回请求的方法

实例如下所示: #coding=utf-8 import json import gevent from django.http import HttpResponse from s...

用python与文件进行交互的方法

本文介绍了用python与文件进行交互的方法,分享给大家,具体如下: 一.文件处理 1.介绍 计算机系统:计算机硬件,操作系统,应用程序 应用程序无法直接操作硬件,通过操作系统来操作文...

python SQLAlchemy 中的Engine详解

python SQLAlchemy 中的Engine详解

先看这张图,这是从官方网站扒下来的。 Engine 翻译过来就是引擎的意思,汽车通过引擎来驱动,而 SQLAlchemy 是通过 Engine 来驱动,Engine 维护了一个连接池(...

pytorch程序异常后删除占用的显存操作

1-删除模型变量 del model_define 2-清空CUDA cache torch.cuda.empty_cache() 3-步骤2(异步)需要一定时间,设置时延...