Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼
閱讀目錄
一般而言,當(dāng)我們需要某些功能的模塊時(shí)(無論是內(nèi)置模塊或自定義功能的模塊),可以通過import module 或者 from * import module的方式導(dǎo)入,這屬于靜態(tài)導(dǎo)入,很容易理解。
而如果當(dāng)我們需要在程序的運(yùn)行過程時(shí)才能決定導(dǎo)入某個(gè)文件中的模塊時(shí),并且這些文件提供了同樣的接口名字,上面說的方式就不適用了,這時(shí)候需要使用python 的動(dòng)態(tài)導(dǎo)入。
importlib使用
如在scripts目錄中保存著一些功能模塊,向外提供類似的接口poc()和腳本描述信息description,需要傳入一個(gè)參數(shù)target,當(dāng)然腳本執(zhí)行的功能是不一樣的,以下只是舉例:
starnight:EXP-M starnight$ ls scripts/
__init__.py __pycache__ test1.py test2.py test3.py
starnight:EXP-M starnight$ cat scripts/test1.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
description = 'it is a test1'
def poc(target):
print('it is a test1')
return True
而我們需要?jiǎng)討B(tài)傳入腳本名,來選用此時(shí)要執(zhí)行的功能:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import importlib
script_name = input('please input script_name : ') # 手動(dòng)輸入腳本名
module = importlib.import_module('scripts.{}'.format(script_name)) # 動(dòng)態(tài)導(dǎo)入相應(yīng)模塊
func = module.poc('') # 執(zhí)行腳本功能
print(module.description) # 獲取腳本描述信息
please input script_name : test1 it is a test1 it is a test1 ... please input script_name : test3 it is a test3 it is a test3
當(dāng)我們動(dòng)態(tài)給定腳本名字時(shí),就會(huì)動(dòng)態(tài)的導(dǎo)入該模塊,執(zhí)行相應(yīng)的功能。
importlib其他介紹
python doc: importlib
importlib中的幾個(gè)函數(shù):__import__、import_module、find_loader、invalidate_caches、reload
"Note Programmatic importing of modules should use import_module() instead of this function."
當(dāng)進(jìn)行編程時(shí),使用import_module,如上使用該模塊。
find_loader用來查找模塊,reload重新載入模塊,invalidate_caches不多介紹了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python?動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)模塊熱更新的方法
- Python動(dòng)態(tài)導(dǎo)入模塊:__import__、importlib、動(dòng)態(tài)導(dǎo)入的使用場(chǎng)景實(shí)例分析
- Python動(dòng)態(tài)導(dǎo)入模塊和反射機(jī)制詳解
- Python 動(dòng)態(tài)導(dǎo)入對(duì)象,importlib.import_module()的使用方法
- Python動(dòng)態(tài)導(dǎo)入模塊的方法實(shí)例分析
- Python 靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入的實(shí)現(xiàn)示例
相關(guān)文章
Ubuntu16安裝Python3.9的實(shí)現(xiàn)步驟
這篇文章主要介紹了Ubuntu16安裝Python3.9的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
django框架使用orm實(shí)現(xiàn)批量更新數(shù)據(jù)的方法
這篇文章主要介紹了django框架使用orm實(shí)現(xiàn)批量更新數(shù)據(jù)的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Django基于orm操作數(shù)據(jù)庫更新數(shù)據(jù)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-06-06
python實(shí)現(xiàn)隨機(jī)梯度下降法
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)隨機(jī)梯度下降法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
python各類經(jīng)緯度轉(zhuǎn)換的實(shí)例代碼
這篇文章主要介紹了python各類經(jīng)緯度轉(zhuǎn)換的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
python 判斷l(xiāng)inux進(jìn)程,并殺死進(jìn)程的實(shí)現(xiàn)方法
今天小編就為大家分享一篇python 判斷l(xiāng)inux進(jìn)程,并殺死進(jìn)程的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07
Python Pygame實(shí)戰(zhàn)之超級(jí)炸彈人游戲的實(shí)現(xiàn)
如今的玩家們?cè)跓o聊的時(shí)候會(huì)玩些什么游戲呢?王者還是吃雞是最多的選擇。但在80、90年代的時(shí)候多是一些很簡(jiǎn)單的游戲:《超級(jí)瑪麗》、《魂斗羅》等。本文將利用Pygame制作另一個(gè)經(jīng)典游戲—炸彈人,感興趣的可以了解一下2022-03-03

