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

相关文章

PyTorch中topk函数的用法详解

PyTorch中topk函数的用法详解

听名字就知道这个函数是用来求tensor中某个dim的前k大或者前k小的值以及对应的index。 用法 torch.topk(input, k, dim=None, largest=...

Python面向对象之继承和多态用法分析

Python面向对象之继承和多态用法分析

本文实例讲述了Python面向对象之继承和多态用法。分享给大家供大家参考,具体如下: Python 类的继承和多态 Python 类的继承 在OOP(Object Oriented Pr...

python实现线程池的方法

本文实例讲述了python实现线程池的方法。分享给大家供大家参考。具体如下: 原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后执行,当然任务队列要加锁,详细请看代码 文件...

Python中shapefile转换geojson的示例

shapefile转换geojson import shapefile import codecs from json import dumps # read the shapefi...

基于TensorFlow常量、序列以及随机值生成实例

TensorFlow 生成 常量、序列和随机值 生成常量 tf.constant()这种形式比较常见,除了这一种生成常量的方式之外,像Numpy一样,TensorFlow也提供了生成...