Python內(nèi)置模塊sys
sys模塊代表了Python解釋器相關(guān)有的信息,主要用來獲取解釋器的信息。下面的方法提供查看sys模塊下的全部程序單元(包括變量和函數(shù)等):
>>> import sys
>>> [elem for elem in dir(sys) if not elem.startswith('_')]
['abiflags', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix',
'breakpointhook', 'builtin_module_names', 'byteorder', 'call_tracing',
'callstats', 'copyright', 'displayhook', 'dont_write_bytecode', 'exc_info',
'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info',
'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_origin_tracking_depth',
'get_coroutine_wrApper', 'getallocatedblocks', 'getcheckinterval',
'getdefaultencoding', 'getdlopenflags', 'getfilesystemencodeerrors',
'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount',
'getsizeof', 'getswitchinterval', 'gettrace', 'hash_info', 'hexversion',
'implementation', 'int_info', 'intern', 'is_finalizing', 'last_traceback',
'last_type', 'last_value', 'maxsize', 'maxunicode', 'meta_path', 'modules',
'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2',
'real_prefix', 'set_asyncgen_hooks', 'set_coroutine_origin_tracking_depth',
'set_coroutine_wrapper', 'setcheckinterval', 'setdlopenflags', 'setprofile',
'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin',
'stdout', 'thread_info', 'version', 'version_info', 'warnoptions']
可以看出,sys模塊提供了大量的屬性和函數(shù),由于有一些功能方法在實(shí)際程序開發(fā)中用的并不多,下面僅介紹常用的屬性和函數(shù)。
- sys.argv: 獲取運(yùn)行Python程序的命令行參數(shù),是一個(gè)列表,第一個(gè)參數(shù)指運(yùn)行的程序本身,每二個(gè)參數(shù)是命令行參數(shù)的第一個(gè)參數(shù),依次類推...

命令行參數(shù)與argv列表元素的對(duì)應(yīng)關(guān)系
# 新建文件argv_test.py, 代碼內(nèi)容如下
import sys
# 輸入argv列表的長度
print(f'argv列表的長度為:{len(sys.argv)}')
# 打印argv的元素
for arg in sys.argv:
print(arg)
# 使用下面命令執(zhí)行該文件
$ python argv_test.py fengqinyang duguqiubai dongfangbubai renwoxing
# 輸出
argv列表的長度為:5
argv_test.py
fengqinyang
duguqiubai
dongfangbubai
renwoxing
- sys.copyright: Python的解釋器的版權(quán)信息
>>> print(sys.copyright)
Copyright (c) 2001-2019 Python Software Foundation.
All Rights Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 1995-2001 Corporation for National Research Initiatives.
All Rights Reserved.
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.
All Rights Reserved.
- sys.executable: Python解釋器在磁盤上的存儲(chǔ)路徑
>>> print(sys.executable)
/Users/david.tian/.virtualenvs/pysparkvenv/bin/python
- sys.getfilesystemencodeing():當(dāng)前系統(tǒng)中python文件的字符集
>>> print(sys.getfilesystemencoding())
utf-8
- sys.getrefcount(object): 返回對(duì)象的引用計(jì)數(shù),當(dāng)object對(duì)象計(jì)數(shù)個(gè)數(shù)為0時(shí),系統(tǒng)會(huì)回收該對(duì)象
>>> print(sys.getrefcount(sys))
57
>>> myfullname = 'davidekaka'
>>> print(sys.getrefcount(myfullname))
2
- sys.getrecursionlimit(): 返回Python解釋器支持的遞歸深度,該屬性可以通過setrecursionlimit()來重新設(shè)置
>>> print(sys.getrecursionlimit())
1000
>>> sys.setrecursionlimit(1100)
>>> print(sys.getrecursionlimit())
1100
>>> sys.setrecursionlimit(1000)
- sys.maxsize: 該屬性指Python支持整數(shù)的最大值,和系統(tǒng)平臺(tái)有關(guān)系統(tǒng)(32位和64位)不同
>>> print(sys.maxsize)
9223372036854775807
- sys.version: 該屬性返回Python解釋器的版本
>>> print(sys.version)
3.7.4 (v3.7.4:e09359112e, Jul 8 2019, 14:54:52)
[Clang 6.0 (clang-600.0.57)]
- sys.platform: 該屬性返回Python解釋器所在的平臺(tái)
>>> print(sys.platform)
darwin
- sys.path: 該屬性指定Python查找模塊的路徑列表
>>> print(sys.path)
['', '/Users/david.tian/.virtualenvs/pysparkvenv/lib/python37.zip',
'/Users/david.tian/.virtualenvs/pysparkvenv/lib/python3.7',
'/Users/david.tian/.virtualenvs/pysparkvenv/lib/python3.7/lib-dynload',
'/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
'/Users/david.tian/.virtualenvs/pysparkvenv/lib/python3.7/site-packages']