python中间模块重载介绍

电子说

1.2w人已加入

描述

python通过reload重载模块动态更新最新代码。

1.1 reload

用法

import moda
from importlib import reload
reload(moda)

描述

python的reload()函数需先从importlib导入。

reload**( moda )****:**moda为模块对象,即需先导入moda才能进行reload。

假设模块A文件,导入了模块B和C,那么重载模块A的时候,不会重载B和C。

需要单独对中间模块B和C进行重载。

文件内容

E**:**\\documents\\F盘\\testreload.py

import os,sys
import modb
import modc

print("run:{}".format(os.path.abspath(__file__)))
print("__name__:{}".format(__name__))
# a='梯阅线条' # 修改前
a='梯阅线条a' # 修改后
print("a={}".format(a))

E**:**\\documents\\F盘\\modb.py

import os,sys
print("run:{}".format(os.path.abspath(__file__)))
print("__name__:{}".format(__name__))

# b='b' # 修改前
b='bb' # 修改后
print("b={}".format(b))

E**:**\\documents\\F盘\\modc.py

import os,sys
print("run:{}".format(os.path.abspath(__file__)))
print("__name__:{}".format(__name__))

# c='c' # 修改前
c='cc' # 修改后
print("c={}".format(c))

示例

# 打开cmd 执行下面示例
E:\\documents\\F盘>python
>>> import testreload
run:E:\\documents\\F盘\\modb.py
__name__:modb
b=b
run:E:\\documents\\F盘\\modc.py
__name__:modc
c=c
run:E:\\documents\\F盘\\testreload.py
__name__:testreload
a=梯阅线条
# 修改a=’梯阅线条a’,b=’bb’,c=’cc’
>>> from importlib import reload
>>> reload(testreload)
run:E:\\documents\\F盘\\testreload.py
__name__:testreload
# 后只更新了reload 模块内容
a=梯阅线条a
 'testreload' from 'E:\\\\documents\\\\F盘\\\\testreload.py'>
# 未更新中间模块内容
>>> testreload.modb.b
'b'
>>> testreload.modc.c
'c'
# 单独reload 中间模块,内容更新成功
>>> reload(testreload.modb)
run:E:\\documents\\F盘\\modb.py
__name__:modb
b=bb
 'modb' from 'E:\\\\documents\\\\F盘\\\\modb.py'>
>>> reload(testreload.modc)
run:E:\\documents\\F盘\\modc.py
__name__:modc
c=cc
 'modc' from 'E:\\\\documents\\\\F盘\\\\modc.py'>
>>> testreload.modb.b
'bb'
>>> testreload.modc.c
'cc'
>>>

1.2 自动重载中间模块

描述

通过遍历模块属性字典 dict ,对类型type为模块的属性,进行重载。

对中间模块再导入的中间模块,也调用此方法进行重载,即递归重载。

从而达到自动重载中间模块。

文件内容

E**:**\\documents\\F盘\\testreload.py

import os,sys
import types
import modb
import modc
from importlib import reload

print("run:{}".format(os.path.abspath(__file__)))
print("__name__:{}".format(__name__))
#a='梯阅线条' # 修改前
a='梯阅线条a' # 修改后
print("a={}".format(a))

def reload_status(mod,lev,lastmod):
   print('reloading:'+ str(lev) + '-'*lev + mod.__name__ + '(in {})'.format(lastmod.__name__))
   
def reload_conv(mod,convd,lev,lastmod):
   if not mod in convd:
       reload_status(mod,lev,lastmod)
       reload(mod)
       convd[mod]=None
       for attr in mod.__dict__.values():
           if type(attr)==types.ModuleType:
               reload_conv(attr,convd,lev+1,mod)
               
def reload_all(*args):
   convd={}
   for arg in args:
       if type(arg)==types.ModuleType:
           reload_conv(arg,convd,0,arg)
           

if __name__ == '__main__':
   import testreload
   reload_all(testreload)

E**:**\\documents\\F盘\\modb.py

import os,sys
print("run:{}".format(os.path.abspath(__file__)))
print("__name__:{}".format(__name__))

# b='b' # 修改前
b='bb' # 修改后
print("b={}".format(b))

E**:**\\documents\\F盘\\modc.py

import os,sys
print("run:{}".format(os.path.abspath(__file__)))
print("__name__:{}".format(__name__))

# c='c' # 修改前
c='cc' # 修改后
print("c={}".format(c))

示例

# 打开cmd 执行下面示例
E:\\documents\\F盘>python
>>> import testreload
run:E:\\documents\\F盘\\modb.py
__name__:modb
b=b
run:E:\\documents\\F盘\\modc.py
__name__:modc
c=c
run:E:\\documents\\F盘\\testreload.py
__name__:testreload
a=梯阅线条
# 修改a=’梯阅线条a’,b=’bb’,c=’cc’
# 调用 reload_all 自动重载中间模块
>>> testreload.reload_all(testreload)
reloading:0testreload(in testreload)
run:E:\\documents\\F盘\\testreload.py
__name__:testreload
# 更新reload的模块内容
a=梯阅线条a
# 展示模块层级关系,数字大的为小的中间模块,in 为导入当前模块的文件模块
reloading:1-os(in testreload)
reloading:2--abc(in os)
reloading:2--sys(in os)
reloading:2--stat(in os)
reloading:2--ntpath(in os)
reloading:3---genericpath(in ntpath)
reloading:1-types(in testreload)
reloading:1-modb(in testreload)
run:E:\\documents\\F盘\\modb.py
__name__:modb
# 更新中间模块内容
b=bb
reloading:1-modc(in testreload)
run:E:\\documents\\F盘\\modc.py
__name__:modc
c=cc
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分