from os.path import exists
print(exists)
print(dir())
print('============================================')
from os.path import exists as e
print(e)
print(dir())
print('============================================')
import os
print(os.path.exists)
print(os.path.__dict__['exists'])
print(dir())
运行结果:
<function exists at 0x7f77489c28c8>
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'exists']
============================================
<function exists at 0x7f77489c28c8>
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'e', 'exists']
============================================
<function exists at 0x7f77489c28c8>
<function exists at 0x7f77489c28c8>
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'e', 'exists', 'os']
模块的查找路径顺序
import sys
for i in sys.path:
print(i)
运行结果:
/home/yzx/PycharmProjects/python
/home/yzx/PycharmProjects/python
/usr/lib/python35.zip
/usr/lib/python3.5
/usr/lib/python3.5/plat-x86_64-linux-gnu
/usr/lib/python3.5/lib-dynload
/home/yzx/PycharmProjects/python/venv/lib/python3.5/site-packages
/home/yzx/PycharmProjects/python/venv/lib/python3.5/site-packages/setuptools-40.8.0-py3.5.egg
/home/yzx/PycharmProjects/python/venv/lib/python3.5/site-packages/pip-19.0.3-py3.5.egg
查看初始化后模块的加载详情:
import sys
for i in sys.modules.items():
print(i)
执行结果:
('abc', <module 'abc' from '/usr/lib/python3.5/abc.py'>)
('_weakref', <module '_weakref' (built-in)>)
('_bootlocale', <module '_bootlocale' from '/usr/lib/python3.5/_bootlocale.py'>)
('stat', <module 'stat' from '/usr/lib/python3.5/stat.py'>)
('_stat', <module '_stat'