1. 程式人生 > >Python 模塊化 模塊搜索順序、重復導入、模塊加載列表(五)

Python 模塊化 模塊搜索順序、重復導入、模塊加載列表(五)

pychar char 先後 read linux dict abc 令行 python


模塊搜索順序、重復導入、模塊加載列表

0x00 模塊搜索順序:

舉例:

#test.py
import sys

for p in sys.path:
print(p)

運行結果:
C:\python //pycharm環境中的Add content roots to PYTHONPATH
C:\python //腳本所在目錄
C:\Users\ihoney\AppData\Local\Programs\Python\Python35\python35.zip //打包,java紮包,避免大量小文件占空間
C:\Users\ihoney\AppData\Local\Programs\Python\Python35\DLLs
C:\Users\ihoney\AppData\Local\Programs\Python\Python35\lib
C:\Users\ihoney\AppData\Local\Programs\Python\Python35
C:\Users\ihoney\AppData\Local\Programs\Python\Python35\lib\site-packages //第三方包安裝路徑
C:\Users\ihoney\AppData\Local\Programs\Python\Python35\lib\site-packages\win32
C:\Users\ihoney\AppData\Local\Programs\Python\Python35\lib\site-packages\win32\lib
C:\Users\ihoney\AppData\Local\Programs\Python\Python35\lib\site-packages\Pythonwin

  

模塊的路徑搜索順序:
程序主目錄,腳本所在目錄
PYTHONPATH目錄,包含python的path路徑:標準庫目錄和第三方包目錄

環境變量:命令行敲的字符串,依次在路徑中搜索

當import 一個模塊時,會依次的在以上路徑順序中查找,找到了就不再往後找了,找不到就導入異常,只搜索指定目錄,不遞歸搜索。

路徑可以是字典、zip文件、egg文件(蟒蛇蛋)。
.egg文件,是由setuptools庫創建的包,添加了元數據(版本號、依賴項等)的zip文件。下一篇文章介紹。

windows優先搜索".",即當前目錄
linux只會從環境變量中的路徑中挨個找。

比如,當我們在本地寫了一個print.py時,windows下模塊搜索順序優先搜索當前目錄,然後才是python的path路徑 --> 標準庫目錄,由於當前目錄下自定義了一個print模塊,所以可能會導致其它模塊print()打印異常


0x01 模塊的重復導入:

1)創建以下三個文件:

#test.py
import test1
import test2

#test1.py
print("this is test1 module")

#test2.py
print("this is test2 module")

運行test.py的結果:
this is test1 module
this is test2 module

  

2)修改下test.py:

#test.py
import test1
import test2
import test1

再運行下查看結果:
this is test1 module
this is test2 module

  上面我們修改test.py後,導入了兩次test1模塊,但解釋器並沒有運行兩次,也就是模塊不會重復的導入。

3)再修改下test.py和test1.py:

#test.py
import test2
import test1

#test1.py
print("this is test1 module")
import test2

輸出結果:
this is test2 module
this is test1 module

  這次我們先在test.py中先後導入了test2、test1模塊,但從輸出結果中看,test1.py中導入的test2模塊也沒有加載初始化,說明程序入口模塊已經導入了某模塊時,其它調用的模塊也不會重復導入該模塊。

4)再修改如下:

#test.py
import test1
print(test1.test2.x)

#test1.py
print("this is test1 module")
import test2
print(dir())

#test2.py
print("this is test2 module")
x=444

運行test.py結果:
this is test1 module
this is test2 module
[‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘test2‘]
444

  這次依然可以通過test1.test2.x的方式訪問test2模塊中的x屬性。

總結:

內存編址
共享物理內存

0x02 模塊加載列表:

import sys
print(sys.modules.keys()) //加載的所有模塊名

dict_keys([‘_codecs_cn‘, ‘_imp‘, ‘stat‘, ‘encodings.gbk‘, ‘builtins‘, ‘winreg‘, ‘_codecs‘, ‘__main__‘, ‘_sitebuiltins‘, ‘_warnings‘, ‘encodings.latin_1‘, ‘sysconfig‘, ‘genericpath‘, ‘errno‘, ‘_multibytecodec‘, ‘encodings.utf_8‘, ‘codecs‘, ‘os‘, ‘_weakrefset‘, ‘_bootlocale‘, ‘sys‘, ‘os.path‘, ‘encodings.aliases‘, ‘_collections_abc‘, ‘encodings‘, ‘ntpath‘, ‘_stat‘, ‘_frozen_importlib‘, ‘site‘, ‘abc‘, ‘_io‘, ‘_thread‘, ‘test2‘, ‘encodings.mbcs‘, ‘nt‘, ‘_frozen_importlib_external‘, ‘_signal‘, ‘_weakref‘, ‘_locale‘, ‘zipimport‘, ‘io‘, ‘marshal‘, ‘test1‘])

  其中部分模塊及模塊指向的位置:

‘__main__‘: <module ‘__main__‘ from ‘C:/python/test.py‘>,
os.path <module ‘ntpath‘ from ‘C:\\Users\\ihoney\\AppData\\Local\\Programs\\Python\\Python35\\lib\\ntpath.py‘>
sysconfig <module ‘sysconfig‘ from ‘C:\\Users\\ihoney\\AppData\\Local\\Programs\\Python\\Python35\\lib\\sysconfig.py‘>
sys <module ‘sys‘ (built-in)>
os <module ‘os‘ from ‘C:\\Users\\ihoney\\AppData\\Local\\Programs\\Python\\Python35\\lib\\os.py‘>
zipimport <module ‘zipimport‘ (built-in)>
test2 <module ‘test2‘ from ‘C:\\python\\test2.py‘>
....

  其中部分模塊是導入sys模塊時,sys模塊調用的模塊,其中的‘__main__‘模塊指向的是當前腳本名。

Python 模塊化 模塊搜索順序、重復導入、模塊加載列表(五)