python通過reload重載模塊動(dòng)態(tài)更新最新代碼。
1.1 reload
用法
import moda
from importlib import reload
reload(moda)
描述
python的reload()函數(shù)需先從importlib導(dǎo)入。
reload**( moda )****:**moda為模塊對(duì)象,即需先導(dǎo)入moda才能進(jìn)行reload。
假設(shè)模塊A文件,導(dǎo)入了模塊B和C,那么重載模塊A的時(shí)候,不會(huì)重載B和C。
需要單獨(dú)對(duì)中間模塊B和C進(jìn)行重載。
文件內(nèi)容
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 執(zhí)行下面示例
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 模塊內(nèi)容
a=梯閱線條a
'testreload' from 'E:\\\\documents\\\\F盤\\\\testreload.py'>
# 未更新中間模塊內(nèi)容
>>> testreload.modb.b
'b'
>>> testreload.modc.c
'c'
# 單獨(dú)reload 中間模塊,內(nèi)容更新成功
>>> 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 自動(dòng)重載中間模塊
描述
通過遍歷模塊屬性字典 dict ,對(duì)類型type為模塊的屬性,進(jìn)行重載。
對(duì)中間模塊再導(dǎo)入的中間模塊,也調(diào)用此方法進(jìn)行重載,即遞歸重載。
從而達(dá)到自動(dòng)重載中間模塊。
文件內(nèi)容
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 執(zhí)行下面示例
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’
# 調(diào)用 reload_all 自動(dòng)重載中間模塊
>>> testreload.reload_all(testreload)
reloading:0testreload(in testreload)
run:E:\\documents\\F盤\\testreload.py
__name__:testreload
# 更新reload的模塊內(nèi)容
a=梯閱線條a
# 展示模塊層級(jí)關(guān)系,數(shù)字大的為小的中間模塊,in 為導(dǎo)入當(dāng)前模塊的文件模塊
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
# 更新中間模塊內(nèi)容
b=bb
reloading:1-modc(in testreload)
run:E:\\documents\\F盤\\modc.py
__name__:modc
c=cc
-
函數(shù)
+關(guān)注
關(guān)注
3文章
4346瀏覽量
63017 -
代碼
+關(guān)注
關(guān)注
30文章
4837瀏覽量
69122 -
python
+關(guān)注
關(guān)注
56文章
4811瀏覽量
85075
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
動(dòng)態(tài)庫封裝成python模塊的方法
python教程之如何使用XlsxWriter模塊創(chuàng)建aexcel表格
![<b class='flag-5'>python</b>教程之如何使用XlsxWriter<b class='flag-5'>模塊</b>創(chuàng)建aexcel表格](https://file.elecfans.com/web1/M00/82/A8/o4YBAFxBlsiAEo8jAAPpKynz7EI641.png)
Python的函數(shù)文件與模塊的程序說明
![<b class='flag-5'>Python</b>的函數(shù)文件與<b class='flag-5'>模塊</b>的程序說明](https://file.elecfans.com/web1/M00/C6/A1/pIYBAF9Z4xaALpuGAAQeMuXCMwE140.png)
python模塊屬性及字符串導(dǎo)入模塊介紹
python包模塊相對(duì)導(dǎo)入from和import介紹1
介紹Python模塊的基礎(chǔ)知識(shí)
Python中telnetlib模塊的基本使用
模塊重載的五種方法
Python 重載與重寫的概念
![<b class='flag-5'>Python</b> <b class='flag-5'>重載</b>與重寫的概念](https://file1.elecfans.com/web2/M00/AC/59/wKgaomVEnZGAZW-0AAEDBXZR6D4322.jpg)
評(píng)論