sys模块
sys模块是与python解释器交互的一个接口
sys.argv
功能:在执行当前文件的时候,传递一些参数到python文件中,用于传参
返回值:无返回值
#脚本内容
import sys
iput = sys.argv
print(iput)
执行结果:
以列表的形式传参到脚本内部(列表第一个元素为脚本本身名)
sys.modules
功能:
sys.modules是一个全局字典,该字典是python启动后就加载在内存中。每当程序员导入新的模块,sys.modules都将记录这些模块。字典sys.modules对于加载模块起到了缓冲的作用。
当某个模块第一次导入,字典sys.modules将自动记录该模块。当第二次再导入该模块时,python会直接到字典中查找,从而加快了程序运行的速度。
返回值:
dic,字典数据类型 ,包含当前已导入的模块信息;key即代表导入的模块名,而value即该模块所在的内存地址,可通过sys.modules[key]的方法得到该key/模块的内存地址
import sys
print(sys.modules)
'''
返回值:
{'re': <module 're' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\re.py'>, 'copyreg': <module 'copyreg' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\copyreg.py'>, 'site': <module 'site' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\site.py'>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, 'encodings.aliases': <module 'encodings.aliases' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\encodings\\aliases.py'>, '_warnings': <module '_warnings' (built-in)>, 'sre_compile': <module 'sre_compile' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\sre_compile.py'>, '_locale': <module '_locale' (built-in)>, 'winreg': <module 'winreg' (built-in)>, '_collections_abc': <module '_collections_abc' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\_collections_abc.py'>, 'sitecustomize': <module 'sitecustomize' from 'C:\\SoftWare\\Program Files\\JetBrains\\PyCharm 2019.1.2\\helpers\\pycharm_matplotlib_backend\\sitecustomize.py'>, 'codecs': <module 'codecs' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\codecs.py'>, 'sysconfig': <module 'sysconfig' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\sysconfig.py'>, 'sys': <module 'sys' (built-in)>, 'ntpath': <module 'ntpath' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\ntpath.py'>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, 'builtins': <module 'builtins' (built-in)>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_stat': <module '_stat' (built-in)>, '_imp': <module '_imp' (built-in)>, '_weakrefset': <module '_weakrefset' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\_weakrefset.py'>, 'nt': <module 'nt' (built-in)>, 'abc': <module 'abc' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\abc.py'>, 'encodings': <module 'encodings' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\encodings\\__init__.py'>, 'zipimport': <module 'zipimport' (built-in)>, 'encodings.latin_1': <module 'encodings.latin_1' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\encodings\\latin_1.py'>, '_signal': <module '_signal' (built-in)>, '_thread': <module '_thread' (built-in)>, 'encodings.utf_8': <module 'encodings.utf_8' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\encodings\\utf_8.py'>, 'os.path': <module 'ntpath' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\ntpath.py'>, 'errno': <module 'errno' (built-in)>, '__main__': <module '__main__' from 'C:/SoftWare/Program Files/JetBrains/PycharmProjects/Py27/classes/day06/practice/sys.py'>, 'genericpath': <module 'genericpath' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\genericpath.py'>, 'encodings.cp437': <module 'encodings.cp437' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\encodings\\cp437.py'>, 'sre_parse': <module 'sre_parse' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\sre_parse.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\_sitebuiltins.py'>, 'sre_constants': <module 'sre_constants' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\sre_constants.py'>, 'io': <module 'io' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\io.py'>, 'os': <module 'os' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\os.py'>, '_sre': <module '_sre' (built-in)>, 'encodings.mbcs': <module 'encodings.mbcs' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\encodings\\mbcs.py'>, '_codecs': <module '_codecs' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'encodings.gbk': <module 'encodings.gbk' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\encodings\\gbk.py'>, '_bootlocale': <module '_bootlocale' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\_bootlocale.py'>, '_io': <module 'io' (built-in)>, 'stat': <module 'stat' from 'C:\\SoftWare\\Program Files (x86)\\Python\\Python35-32\\lib\\stat.py'>, '_multibytecodec': <module '_multibytecodec' (built-in)>, 'marshal': <module 'marshal' (built-in)>}
'''
上述字典中的模块即当前加载的模块,可见上述返回字典中无法找到datatime模块信息,是因为目前未加载使用过该模块,若在代码中通过"import datetime"导入datetime模块,即可在返回的字典中找到
sys.path
功能:
1.能否导入一个模块,就要看这个模块的所在的路径是否在sys.path中;如果想要导入某个模块,则可通过sys.path.append(modle_path)的方法将模块路径添加到sys.path中
2.如果在sys.path寻找数据的时候,如果能找到一个文件,那么就不继续往下走了
返回值:list 列表;python搜索模块的路径集
# / 路径下运行该脚本
import sys
print(sys.path)
'''
返回值:
['/', '/usr/local/lib/python35.zip', '/usr/local/lib/python3.5', '/usr/local/lib/python3.5/plat-linux', '/usr/local/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/site-packages']
'''