开发

大师兄的Python源码学习笔记(三十五): 模块的动态加载机制

2021-09-30  本文已影响0人  superkmi

大师兄的Python源码学习笔记(三十四): 模块的动态加载机制(一)
大师兄的Python源码学习笔记(三十六): 模块的动态加载机制(三)

二、import机制的黑盒探测

import pandas
import pandas.arrays
from pandas import Dataframe
from pandas import Dataframe as df
from pandas import *
1. 标准import
1.1 Python内建Module
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>>> import sys
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'sys']
>>> type(sys)
<class 'module'>
demo.py

>>>import sys

>>>def show_modules():
>>>    for item in sys.modules.items():
>>>        print(item)

>>>show_modules()
('sys', <module 'sys' (built-in)>)
('builtins', <module 'builtins' (built-in)>)
('_frozen_importlib', <module 'importlib._bootstrap' (frozen)>)
('_imp', <module '_imp' (built-in)>)
('_warnings', <module '_warnings' (built-in)>)
('_frozen_importlib_external', <module 'importlib._bootstrap_external' (frozen)>)
('_io', <module 'io' (built-in)>)
... ...
demo.py

>>> import sys
>>> id(sys.modules['os'])
1755873529264
>>> import os
>>> id(os)
1755873529264
1.2 用户自定义Module
test.py

>>>a=1
>>>b=2
demo.py 

>>>import sys

>>>def test_in_modules():
>>>    return print("test" in sys.modules.keys())

>>>test_in_modules()
False
>>>import test
>>>test_in_modules()
True
>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys', 'test', 'test_in_modules']
>>>print(id(test))
2018381998256
>>>print(id(sys.modules['test']))
2018381998256
>>>print(type(test))
<class 'module'>
>>>import test
>>>print(dir(test))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b']
>>>print(dir(test.__dict__.keys()))
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'isdisjoint']
>>>print(test.__name__)
test
>>>print(test.__file__)
.\test.py
demo.py

>>>import test
>>>print(type(__builtins__))
<class 'module'>
>>>print(id(__builtins__))
1761693991696
>>>print(type(test.__builtins__))
<class 'dict'>
>>>print(id(test.__builtins__))
1761693989184
demo.py

import test
>>>print(id(test.__builtins__))
2325670918464
>>>print(id(__builtins__.__dict__))
2325670918464
>>>print(id(sys.modules['builtins'].__dict__))
2325670918464
2. 嵌套import
test1.py

import sys
test2.py

import test1
demo.py

>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys']
>>>import test2
>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys', 'test2']
>>>print(dir(test2))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'test1']
>>>print(dir(test2.test1))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys']
demo.py

>>>import test2
>>>print(sys.modules['test2'])
<module 'test2' from 'D:\\pythonProject\\parser_learn\\test2.py'>
demo.py

>>>import test2,test1

>>>print(id(test1))
2555034952864
>>>print(id(test2.test1))
2555034952864
3. import package
mypackage.test.py

>>>a=1
>>>b=2
demo.py

import mypackage

>>>print(mypackage)
<module 'mypackage' (namespace)>
>>>print(mypackage.test)
Traceback (most recent call last):
  File "D:/demo.py", line 4, in <module>
    print(mypackage.test)
AttributeError: module 'mypackage' has no attribute 'test'
mypackage.__init__.py

from . import test
demo.py 

import mypackage

>>>print(mypackage)
<module 'mypackage' from 'D:\\mypackage\\__init__.py'>
>>>print(mypackage.test)
<module 'mypackage.test' from 'D:\\mypackage\\test.py'>
demo.py

>>>import sys
>>>import mypackage.test

>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'mypackage', 'sys']
>>>import mypackage
>>>print(dir(mypackage))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'test']
>>>print(id(mypackage.test))
2378910617488
>>>print(id(sys.modules['mypackage.test']))
2378910617488
mypackage.test1

>>>a=1
>>>b=2
mypackage.test2

>>>c=3
>>>d=4
demo.py

>>>import mypackage.test1
>>>print(dir(mypackage))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'test1']
>>>import mypackage.test2
>>>print(dir(mypackage))
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'test1', 'test2']
4. from 与 import
demo.py 

>>>import sys
>>>from mypackage import test1
>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys', 'test1']
>>>print(sys.modules['mypackage.test1'])
<module 'mypackage.test1' from 'D:\\mypackage\\test1.py'>

import mypackage.test1中,Python虚拟机引入了符号mypackage,并将其映射到module mypackage。
from mypackage import test1中,Python虚拟机引入了符号test,并将其映射到module mypackage.test。

demo.py

>>>import sys
>>>from mypackage.test1 import a
>>>print(a)
1
>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'sys']
>>>print(sys.modules['mypackage.test1'])
<module 'mypackage.test1' from 'D:\\mypackage\\test1.py'>
demo.py

>>>from mypackage.test1 import *
>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b']
5. 符号重命名
demo.py

>>>import sys
>>>import mypackage.test1 as test

>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys', 'test']
>>>print(sys.modules['mypackage.test1'])
<module 'mypackage.test1' from 'D:\\mypackage\\test1.py'>
6. 符号的销毁与重载
demo.py

>>>import sys
>>>import mypackage.test1 as test

>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys', 'test']
>>>del test
>>>print(dir())
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'sys']
>>>print(sys.modules['mypackage.test1'])
<module 'mypackage.test1' from 'D:\\pythonProject\\parser_learn\\mypackage\\test1.py'>
demo.py

>>> import importlib,sys
>>> import mypackage.test1 as test
>>> id(test)
2543593826720
>>> sys.modules['mypackage.test1']
<module 'mypackage.test1' from 'D:\\mypackage\\test1.py'>
>>> dir(sys.modules['mypackage.test1'])
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b']
>>> importlib.reload(test) # 这里module test发生了变化
<module 'mypackage.test1' from 'D:\\mypackage\\test1.py'>
>>> dir(sys.modules['mypackage.test1'])
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'b', 'c']
>>> id(test)
2543593826720
上一篇 下一篇

猜你喜欢

热点阅读