最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python內(nèi)置函數(shù)——__import__ 的使用方法

 更新時間:2017年11月24日 09:19:12   作者:十月狐貍  
本篇文章主要介紹了Python內(nèi)置函數(shù)——__import__ 的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

__import__() 函數(shù)用于動態(tài)加載類和函數(shù) 。

如果一個模塊經(jīng)常變化就可以使用 __import__() 來動態(tài)載入。

語法

__import__ 語法:

__import__(name[, globals[, locals[, fromlist[, level]]]])

參數(shù)說明:

name -- 模塊名

英文文檔:

__import__(name, globals=None, locals=None, fromlist=(), level=0)

This function is invoked by the import statement. It can be replaced (by importing the builtins module and assigning to builtins.__import__) in order to change semantics of the import statement, but doing so is strongly discouraged as it is usually simpler to use import hooks (see PEP 302) to attain the same goals and does not cause issues with code which assumes the default import implementation is in use. Direct use of __import__() is also discouraged in favor of importlib.import_module().

The function imports the module name, potentially using the given globals and locals to determine how to interpret the name in a package context. The fromlist gives the names of objects or submodules that should be imported from the module given by name. The standard implementation does not use its locals argument at all, and uses its globals only to determine the package context of the import statement.

level specifies whether to use absolute or relative imports. 0 (the default) means only perform absolute imports. Positive values for level indicate the number of parent directories to search relative to the directory of the module calling __import__() (see PEP 328 for the details).

When the name variable is of the form package.module, normally, the top-level package (the name up till the first dot) is returned, not the module named by name. However, when a non-empty fromlist argument is given, the module named by name is returned.

說明:

  1. 函數(shù)功能用于動態(tài)的導(dǎo)入模塊,主要用于反射或者延遲加載模塊。

  2. __import__(module)相當于import module

先定義兩個模塊mian.py和index.py,兩個文件在同一目錄下:

#index.py
print ('index')

def sayHello():
  print('hello index')

def sayHelloZhCn():
  print('你好 index')

#mian.py
print ('main')

index = __import__('index')
dir(index)
index.sayHello()
index.sayHelloZhCn()

執(zhí)行main.py,可以證實動態(tài)加載了index.py,__import__返回的模塊也是index模塊

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
index
hello index
你好 index

3. __import__(package.module)相當于from package import name,如果fromlist不傳入值,則返回package對應(yīng)的模塊,如果fromlist傳入值,則返回package.module對應(yīng)的模塊。

先定義archives包,其中包含user和role兩個模塊:

#__index__.py
print ('archives.__index__')

def sayHello():
  print('hello archives')
#user.py
print ('user')

def sayHello():
  print('hello user')
#role.py
print ('role')

def sayHello():
  print('hello role')

結(jié)構(gòu)如下:

修改mian.py:

#main.py
print ('main')

archives = __import__('archives')
archives.sayHello()
archives.user

執(zhí)行main.py,可以證實動態(tài)加載了archives包,__import__返回的模塊也是archives模塊

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
hello archives
Traceback (most recent call last):
  File "main.py", line 5, in <module>
    archives.user
AttributeError: module 'archives' has no attribute 'user'

修改mian.py:

#main.py
print ('main')

archives = __import__('archives.user')
archives.sayHello()
print(archives.user)

執(zhí)行main.py,可以證實動態(tài)加載了archives包的user模塊,__import__返回的模塊也是archives模塊

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
user
hello archives
<module 'archives.user' from 'C:\\Users\\Admin\\Documents\\Python3\\import
test\\archives\\user.py'>

修改mian.py:

#main.py
print ('main')

archives = __import__('archives.user',fromlist = ('user',))
archives.sayHello()
print(archives)

執(zhí)行main.py,可以證實動態(tài)加載了archives包的user模塊,__import__返回的模塊是user模塊

C:\Users\Admin\Documents\Python3\importtest>python main.py
main
archives.__index__
user
hello user
<module 'archives.user' from 'C:\\Users\\Admin\\Documents\\Python3\\import
test\\archives\\user.py'>

4. level參數(shù),指定是使用絕對導(dǎo)入還是相對導(dǎo)入。 0(默認值)表示只執(zhí)行絕對導(dǎo)入。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python如何為創(chuàng)建大量實例節(jié)省內(nèi)存

    python如何為創(chuàng)建大量實例節(jié)省內(nèi)存

    這篇文章主要為大家詳細介紹了python如何為創(chuàng)建大量實例節(jié)省內(nèi)存,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Python Pandas pandas.read_sql_query函數(shù)實例用法分析

    Python Pandas pandas.read_sql_query函數(shù)實例用法分析

    在本篇文章里小編給大家整理的是一篇關(guān)于Python Pandas pandas.read_sql_query函數(shù)實例用法分析內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。
    2021-06-06
  • 詳解Python進程間通信之命名管道

    詳解Python進程間通信之命名管道

    本篇文章主要介紹了詳解Python進程間通信之命名管道,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 使用Python操作Jenkins的過程詳解

    使用Python操作Jenkins的過程詳解

    借助于Python中的python-jenkins模塊,我們可以輕松地編寫腳本來連接到Jenkins服務(wù)器,并執(zhí)行各種操作,如創(chuàng)建、刪除、構(gòu)建Jobs等,這種自動化的方式不僅提高了效率,還使得CI/CD流程更加靈活和可控,本文介紹如何使用Python操作Jenkins的相關(guān)資料,需要的朋友可以參考下
    2024-05-05
  • python3 如何解壓縮.gz文件

    python3 如何解壓縮.gz文件

    這篇文章主要介紹了python3 如何解壓縮.gz文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Python創(chuàng)建對稱矩陣的方法示例【基于numpy模塊】

    Python創(chuàng)建對稱矩陣的方法示例【基于numpy模塊】

    這篇文章主要介紹了Python創(chuàng)建對稱矩陣的方法,結(jié)合實例形式分析了Python基于numpy模塊實現(xiàn)矩陣運算的相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10
  • 使用Python實現(xiàn)網(wǎng)絡(luò)設(shè)備配置備份與恢復(fù)

    使用Python實現(xiàn)網(wǎng)絡(luò)設(shè)備配置備份與恢復(fù)

    網(wǎng)絡(luò)設(shè)備配置備份與恢復(fù)在網(wǎng)絡(luò)安全管理中起著至關(guān)重要的作用,本文為大家介紹了如何通過Python實現(xiàn)網(wǎng)絡(luò)設(shè)備配置備份與恢復(fù),需要的可以參考下
    2025-03-03
  • python修改pip install默認安裝路徑的詳細步驟

    python修改pip install默認安裝路徑的詳細步驟

    pip安裝的第三方庫默認存放在C盤中,為了便于管理和不過度占用C盤空間所以想修改默認的pip路徑,文章通過圖文結(jié)合的方式給大家介紹的非常詳細,需要的朋友可以參考下
    2025-04-04
  • Python自動化實戰(zhàn)之接口請求的實現(xiàn)

    Python自動化實戰(zhàn)之接口請求的實現(xiàn)

    本文為大家重點介紹如何通過 python 編碼來實現(xiàn)我們的接口測試以及通過Pycharm的實際應(yīng)用編寫一個簡單接口測試,感興趣的可以了解一下
    2022-05-05
  • Python使用PIL庫實現(xiàn)驗證碼圖片的方法

    Python使用PIL庫實現(xiàn)驗證碼圖片的方法

    這篇文章主要介紹了Python使用PIL庫實現(xiàn)驗證碼圖片的方法,結(jié)合實例形式較為詳細的分析了Python基于PIL庫生成驗證碼圖片的相關(guān)技巧與注意事項,需要的朋友可以參考下
    2016-03-03

最新評論

哈密市| 民县| 昔阳县| 西城区| 青海省| 云阳县| 韶山市| 商水县| 宾川县| 保靖县| 合水县| 游戏| 岢岚县| 邹平县| 克什克腾旗| 香港| 盘山县| 金寨县| 于都县| 会同县| 顺义区| 柳林县| 玉溪市| 皮山县| 灵寿县| 保德县| 长春市| 玉溪市| 垦利县| 买车| 资兴市| 怀宁县| 兴山县| 夏津县| 建宁县| 三门县| 锦屏县| 灵石县| 中超| 临朐县| 靖边县|