新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Python __import__()
內(nèi)置函數(shù)__import__()由 import 語句調(diào)用。Import 語句是調(diào)用導(dǎo)入機(jī)制的最常見方式。這是一個很少使用的方法,它也有助于在運(yùn)行時導(dǎo)入模塊。

**__import__(name, globals=None, locals=None, fromlist=(), level=0)** #where name indicates the name of the import module
_ 導(dǎo)入 _()參數(shù):
取五個參數(shù)。當(dāng)語句調(diào)用此函數(shù)時,此函數(shù)可以更改導(dǎo)入語句的語義。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 名字 | 要導(dǎo)入的模塊的名稱 | 需要 |
| 全球和本地 | 確定如何在包上下文中解釋名稱。默認(rèn)值為無。 | 可選擇的 |
| 從列表 | 應(yīng)按名稱導(dǎo)入的對象或子模塊 | 可選擇的 |
| 水平 | 指定是使用絕對導(dǎo)入還是相對導(dǎo)入。默認(rèn)值為 0 | 可選擇的 |
_ 導(dǎo)入 _()返回值
在按名稱導(dǎo)入模塊的情況下,我們可以使用importlib.import_module()。如果找不到指定的模塊,將會出現(xiàn) ModuleNotFoundError。
| 投入 | 返回值 | | If 參數(shù) | 導(dǎo)入對象 |
Python 中__import__()方法的示例
示例 1:在 python 中__import()__ 是如何工作的?
maths = __import__('math', globals(), locals(), [], 0)
print(maths.fabs(-3.5))
輸出:
3.5示例 2:在 python 中使用__import()__ 的方法
rand = __import__('random')
print(rand.randint(0,10))
print(rand.randint(0,10))
輸出:
6
2 當(dāng)前文章:創(chuàng)新互聯(lián)Python教程:Python __import__()
URL網(wǎng)址:http://www.5511xx.com/article/cohijis.html


咨詢
建站咨詢
