Python內置函數——__import__ 的使用方法
__import__() 函數用于動態(tài)加載類和函數 。
如果一個模塊經常變化就可以使用 __import__() 來動態(tài)載入。
語法
__import__ 語法:
__import__(name[, globals[, locals[, fromlist[, level]]]])
參數說明:
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. 函數功能用于動態(tài)的導入模塊,主要用于反射或者延遲加載模塊。
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對應的模塊,如果fromlist傳入值,則返回package.module對應的模塊。
先定義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')
結構如下:

修改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參數,指定是使用絕對導入還是相對導入。 0(默認值)表示只執(zhí)行絕對導入。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
詳解如何優(yōu)化和調整Python中Scrapy的性能
在本篇高級教程中,我們將深入探討如何優(yōu)化和調整Scrapy爬蟲的性能,以及如何處理更復雜的抓取任務,如登錄,處理Cookies和會話,以及避免爬蟲被網站識別和封鎖,需要的朋友可以參考下2023-09-09
python基于exchange函數發(fā)送郵件過程詳解
這篇文章主要介紹了python基于exchange函數發(fā)送郵件過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-11-11
解決pyecharts運行后產生的html文件用瀏覽器打開空白
這篇文章主要介紹了解決pyecharts運行后產生的html文件用瀏覽器打開空白,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03

