前言
此模塊提供一種可以訪問需要使用的或者被python解釋器或者函數等可以與python解釋器進行交互的對象
注意:此模塊是與python解釋器相關的,並不是與操作系統相關,與操作系統相關的是os模塊
方法列表
方法
意義
sys.argv
命令行參數,在運行python程序時,可以添加一些參數,參數可用於python程序運行時的特定執行動作等,所有參數會在python中存入爲一個列表,其中第一個參數爲python程序本身,也是列表中的第一個元素,可以使用sys.argv[0]顯示出來
sys.exit(n)
程序退出狀態碼,通常情況下,退出狀態碼爲0時爲正常退出,其它數字都爲異常退出
sys.version
獲取python安裝程序的版本信息
sys.maxint
python支持的最大的整數值
sys.path
模塊的搜索路徑,path的環境變量,path[0]爲腳本目錄
sys.platform
獲取python當着運行的操作系統平臺,通常可用於編寫跨平臺的腳本程序,
sys.abiflags
在POSIX系統中,Python使用了標準的配置腳本,且使用了ABI的標記。
sys.base_exec_prefix
Python執行環境的前綴路徑
sys.base_prefix
python的基礎路徑
sys.builtin_module_names
sys內置的模塊名稱,返回爲一個元組
sys.byteorder
An indicator of the native byte order. This will have the value ‘big’ on big-endian (most-significant byte first) platforms, and ‘little’ on little-endian (least-significant byte first) platforms.
sys.copyright
Python軟件的版本信息
sys.displayhook
是一種評估表達式進入到python交互模式的結果,
sys.dont_write_bytecode
如果結果爲“True”,Python不會去嘗試寫入.pyc或者.pyo的文件在導入到源模塊中,初始值被設置成“True”,“False”取決於“-B”命令行選項,和“PYTHONDONTWRITEBYTECODE”的環境變量,可以用戶自定義
sys.flags
是一種順序結構的標誌,用於展示命令行標誌的狀態,這些屬性是“只讀”的,https://blog.csdn.net/Huangfei10086/article/details/106452099
sys.float_info
一種關於浮點數據類型的順序結構的信息。它包含低級別的精確度和內部特徵的信息,這些值 用於在在C語言中的頭文件float.h對應浮點型常量數據。
sys.float_repr_style
一段字符串用於標明repr()函數是如何爲浮點數工作的,short表示是有限的小數x.此外還有legacy選項
sys.getfilesystemencoding()
返回編碼格式的名稱,通常用於使用系統的編碼來覆蓋Python的unicode編碼,不同的操作系統的編碼不一樣
sys.getdefaultencoding()
返回當前默認的字符串編碼格式,這個編碼格式會被unicode使用
sys.getcheckinterval()
返回python解釋器的檢查時間,這個“檢查時間”主要用於python解釋器檢查線程交換和信號處理的時間週期
sys.getdlopenflags()
返回dlopen()調用所使用到的當前的標誌的值,這個標誌常量定義在ctypes和ELFCN模塊中,僅用於Unix
sys.getprofile()
獲取python語法檢查器
sys.getrecursionlimit()
獲取當前遞規循環的最大限制,也就是Python 棧的最大深度,此限制用於阻止無限的遞規循環而導致溢出而崩潰,可以使用setrecursionlimit()
sys.getrefcount()
返回一個對象被引用的次數,此次數通常比你預期的大,因爲會被一些臨時文件參考引用
sys.getsizeof()
以字節爲計數單位返回對象的大小,對象可以是任意對象,所有內建的對象會被返回一個正確的結果,但是此方法不能獲得第三方擴展的真實準確的數字
sys.getswitchinterval()
在前面有一個sys.getcheckinterval()已經被放棄了,現在的新方法爲sys.getswitchinterval(),方法用於返回Python的線程交換時間,可以使用sys.setswitchinterval()來設置時間,單位爲秒
sys.gettotalrefcount()
獲取所有的被參考引用的次數
sys.gettrace()
獲取追蹤信息,通常用於debugger
sys.getwindowsversion()
返回一個元組,描述當前Windows運行的版本信息,元素的名稱爲:major,minor,build,platform,service_pack_mirror,service_pack_major,suite_mask,product_type. 其中service_pack是字符串,其它的均爲數字這些組件可以使用名稱進行訪問,如sys.getwindowsversion()[0],相當於sys.getwindowsversion().major。
===================
=================================================================================
sys.hash_info
一個順序結構的給定數字參數hash的實現,
sys.hexversion
python版本號,以十六進制方式顯示,
sys.implementation
以名稱空間(namespace)爲名稱的一個元組,其中包含版本,十六進制版本等
sys.int_info
一個順序結構,用於展示python的內部關於十進制整數的信息,屬性爲只讀,
sys.is_finalizing()
如果python退出,則返回True,具體意義有待查看
sys.intern(string)
輸入一個字符串,此字符串位於“interned”字符串中,並返回該字符串,這個字符串可能是它本身,也可以是一份拷貝,內部字符串有助於提高字典查詢性能。如果字典的key是interned的,key的比較(哈希後)可以被一個指針比較,而不是字符串比較。通常的,被python程序使用的名稱自動是interned的。且字典使用此模塊,類和實例擁有interned有key.
===================
=================================================================================
sys.maxsize
Py_ssize_t 類型的變量的最大數值,在32位操作系統中,通常是231-1、64位操作系統中是2 63-1
sys.maxunicode
unicode 所支持的最大的數值,此數值依賴配置選項,選項有UCS-2或者UCS-4
sys.modules
輸出一個字典,將那些已經加載的模塊名稱映射到模塊本身,這可以操作強制重新加載模塊或者其它的使用技巧
sys.platform
返回當前python程序所運行的平臺,此方法多用於同一個程序可以在多個不同的平臺上都執行,多用於跨平臺的腳本編寫時用於識別當前平臺
sys.path
Python 搜索模塊時的路徑,返回爲一個列表,初始時爲一個環境變量“PYTHONPATH”,程序開啓時會被加載。因爲是一個列表,所以可以使用下標進行調用
sys.path_hooks
一個可被調用的列表,此列表可以將一個路徑的參數 用來嘗試去創建一個路徑的finder如果一個finder可能被創建,就會被此調用返回,否則將會報錯.
sys.path_importer_cache
finder對象的字典緩存,字典的key都是已經被傳入到sys.path_hooks,值是被找到的finder對象。
sys.prefix
一段字符串,是站點指定的字典前綴, 是平臺安裝python時獨立的文件目錄,
sys.ps1
python解釋器的primary提示符,僅用於python解釋器在交互模式時,初始的值爲’>>> ’ 和 '… ’
sys.ps2
python解釋器的Secondary提示符,僅用於python解釋器在交互模式時,初始的值爲’>>> ’ 和 '… ’
===================
=================================================================================
sys.setcheckinterval()
在前面getcheckinterval()時有對應說到setcheckinterval()主要用於設置python線程間切換的時間
sys.setprofile()
設置系統的profile函數,允許人爲去實施一個Python源代碼的語法分析。
sys.setrecursionlimit()
設置最大棧的深度限制,防止像遞規循環等無限制的壓棧而導致棧溢出。
sys.setswitchinterval()
設置線程切換的時間
sys.settrace()
設置系統的trace的函數,允許人爲去實施一個Python的debugger,此函數是針對特定線程的,它必需註冊使用settrace()對每個線程開啓debugger
sys.stderr
標準錯誤輸出
sys.stdin
標準輸入,用於所有交互式的輸入(包含調用input()
sys.stdout
標準輸出,用於輸出,print();表達式和input()提示符
sys.version_info
一個元組,包含版本信息的5個元素,major,minor,micro,releasevel,和serial。所有信息除了releaselevel外都是數字,release的級別爲 alpha,beta,candidate或者是final;version_info值用於對應python的版本。
sys.warnoptions
sys.stdout.write(“please”)
標準輸出的字符串
sys.base_exec_prefix
Linux 下
>> > import sys
>> > print ( sys. base_exec_prefix)
/ usr/ local
>> >
[ root@Private ~]
/usr/local/bin/python3
Windows下
import sys
print ( sys. base_exec_prefix)
"""
D:\Program Files\Python3.8
"""
sys.builtin_module_names
Windows 下
import sys
print ( sys. builtin_module_names)
"""
('_abc', '_ast', '_bisect', '_blake2', '_codecs', '_codecs_cn', '_codecs_hk', '_codecs_iso2022', '_codecs_jp', '_codecs_kr', '_codecs_tw', '_collections', '_contextvars', '_csv', '_datetime', '_functools', '_heapq', '_imp', '_io', '_json', '_locale', '_lsprof', '_md5', '_multibytecodec', '_opcode', '_operator', '_pickle', '_random', '_sha1', '_sha256', '_sha3', '_sha512', '_signal', '_sre', '_stat', '_statistics', '_string', '_struct', '_symtable', '_thread', '_tracemalloc', '_warnings', '_weakref', '_winapi', '_xxsubinterpreters', 'array', 'atexit', 'audioop', 'binascii', 'builtins', 'cmath', 'errno', 'faulthandler', 'gc', 'itertools', 'marshal', 'math', 'mmap', 'msvcrt', 'nt', 'parser', 'sys', 'time', 'winreg', 'xxsubtype', 'zlib')
"""
Linux 下
>> > import sys
>> > print ( sys. builtin_module_names)
( '_abc' , '_ast' , '_codecs' , '_collections' , '_functools' , '_imp' , '_io' , '_locale' , '_operator' , '_signal' , '_sre' , '_stat' , '_string' , '_symtable' , '_thread' , '_tracemalloc' , '_warnings' , '_weakref' , 'atexit' , 'builtins' , 'errno' , 'faulthandler' , 'gc' , 'itertools' , 'marshal' , 'posix' , 'pwd' , 'sys' , 'time' , 'xxsubtype' )
>> >
sys.byteorder
import sys
print ( sys. byteorder)
"""
little
"""
sys.copyright
import sys
print ( sys. copyright)
"""
Copyright (c) 2001-2020 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.displayhook
import sys
print ( sys. displayhook)
"""
<built-in function displayhook>
"""
sys.flags 直接輸出
import sys
print ( sys. flags)
"""
sys.flags(debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, quiet=0, hash_randomization=1, isolated=0, dev_mode=False, utf8_mode=0)
"""
sys.flag的標誌
attribute
flag
debug
-d
division_warning
-Q
inspect
-i
interactive
-i
optimize
-O or -OO
dont_write_bytecode
-B
no_user_site
-s
no_site
-S
ignore_environment
-E
verbose
-v
bytes_warning
-b
quiet
-q
hash_randomization
-R
sys.float_info
一種關於浮點數據類型的順序結構的信息。它包含低級別的精確度和內部特徵的信息,這些值 用於在在C語言中的頭文件float.h對應浮點型常量數據。
sys.float_info 運行信息
import sys
print ( sys. float_info)
"""
sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)
"""
sys float_info的宏
attribute
float.h macro
explanation
epsilon
DBL_EPSILON
difference between 1 and the least value greater than 1 that is representable as a float
dig
DBL_DIG
maximum number of decimal digits that can be faithfully represented in a float; see below
mant_dig
DBL_MANT_DIG
float precision: the number of base-radix digits in the significand of a float
max
DBL_MAX
maximum representable finite float
max_exp
DBL_MAX_EXP
maximum integer e such that radix**(e-1) is a representable finite float
max_10_exp
DBL_MAX_10_EXP
maximum integer e such that 10**e is in the range of representable finite floats
min
DBL_MIN
minimum positive normalized float
min_exp
DBL_MIN_EXP
minimum integer e such that radix**(e-1) is a normalized float
min_10_exp
DBL_MIN_10_EXP
minimum integer e such that 10**e is a normalized float
radix
FLT_RADIX
radix of exponent representation
rounds
FLT_ROUNDS
integer constant representing the rounding mode used for arithmetic operations. This reflects the value of the system FLT_ROUNDS macro at interpreter startup time. See section 5.2.4.2.2 of the C99 standard for an explanation of the possible values and their meanings.
sys.float_repr_style
import sys
print ( sys. float_repr_style)
"""
short
"""
sys.getfilesystemencoding()
import sys
print ( sys. getfilesystemencoding( ) )
"""
utf-8
"""
sys.getdefaultencoding()
import sys
print ( sys. getdefaultencoding( ) )
"""
utf-8
"""
sys.getcheckinterval()
Windows
import sys
print ( sys. getcheckinterval( ) )
"""
100
E:/Nextcloud/NAS/Study/Study-Note/Python/Pycharm/Lab/week5/systest.py:9: DeprecationWarning: sys.getcheckinterval() and sys.setcheckinterval() are deprecated. Use sys.getswitchinterval() instead.
print(sys.getcheckinterval())
"""
Linux
>> > import sys
>> > print ( sys. getcheckinterval( ) )
< stdin> : 1 : DeprecationWarning: sys. getcheckinterval( ) and sys. setcheckinterval( ) are deprecated. Use sys. getswitchinterval( ) instead.
100
>> >
sys.getrecursionlimit() 遞規層數限制
import sys
print ( sys. getrecursionlimit( ) )
"""
1000
"""
sys.getswitchinterval()
import sys
print ( sys. getswitchinterval( ) )
"""
0.005
"""
import sys
print ( sys. getwindowsversion( ) )
"""
sys.getwindowsversion(major=10, minor=0, build=18363, platform=2, service_pack='')
"""
import sys
print ( sys. hash_info)
"""
sys.hash_info(width=32, modulus=2147483647, inf=314159, nan=0, imag=1000003, algorithm='siphash24', hash_bits=64, seed_bits=128, cutoff=0)
"""
sys.implementation
import sys
print ( sys. version)
print ( sys. hexversion)
print ( sys. implementation)
"""
3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)]
50856688
namespace(cache_tag='cpython-38', hexversion=50856688, name='cpython', version=sys.version_info(major=3, minor=8, micro=2, releaselevel='final', serial=0))
"""
sys.int_info
Attribute
Explanation
bits_per_digit
number of bits held in each digit. Python integers are stored internally in base 2**int_info.bits_per_digit
sizeof_digit
size in bytes of the C type used to represent a digit
import sys
print ( sys. int_info)
"""
sys.int_info(bits_per_digit=15, sizeof_digit=2)
"""
sys.maxsize
import sys
print ( sys. maxsize)
"""
2147483647
"""
sys.maxunicode
import sys
print ( sys. maxunicode)
"""
1114111
"""
sys.meta_path
import sys
print ( sys. meta_path)
"""
[<class '_frozen_importlib.BuiltinImporter'>, <class '_frozen_importlib.FrozenImporter'>, <class '_frozen_importlib_external.PathFinder'>]
"""
sys.modules
import sys
print ( sys. modules)
"""
{'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module '_frozen_importlib' (frozen)>, '_imp': <module '_imp' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_frozen_importlib_external': <module '_frozen_importlib_external' (frozen)>, '_io': <module 'io' (built-in)>, 'marshal': <module 'marshal' (built-in)>, 'nt': <module 'nt' (built-in)>, '_thread': <module '_thread' (built-in)>, '_weakref': <module '_weakref' (built-in)>, 'winreg': <module 'winreg' (built-in)>, 'time': <module 'time' (built-in)>, 'zipimport': <module 'zipimport' (frozen)>, '_codecs': <module '_codecs' (built-in)>, 'codecs': <module 'codecs' from 'D:\\Program Files\\Python 3.8.2\\lib\\codecs.py'>, 'encodings.aliases': <module 'encodings.aliases' from 'D:\\Program Files\\Python 3.8.2\\lib\\encodings\\aliases.py'>, 'encodings': <module 'encodings' from 'D:\\Program Files\\Python 3.8.2\\lib\\encodings\\__init__.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'D:\\Program Files\\Python 3.8.2\\lib\\encodings\\utf_8.py'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' from 'E:/Nextcloud/NAS/Study/Study-Note/Python/Pycharm/Lab/week5/test.py'>, 'encodings.latin_1': <module 'encodings.latin_1' from 'D:\\Program Files\\Python 3.8.2\\lib\\encodings\\latin_1.py'>, '_abc': <module '_abc' (built-in)>, 'abc': <module 'abc' from 'D:\\Program Files\\Python 3.8.2\\lib\\abc.py'>, 'io': <module 'io' from 'D:\\Program Files\\Python 3.8.2\\lib\\io.py'>, '_stat': <module '_stat' (built-in)>, 'stat': <module 'stat' from 'D:\\Program Files\\Python 3.8.2\\lib\\stat.py'>, '_collections_abc': <module '_collections_abc' from 'D:\\Program Files\\Python 3.8.2\\lib\\_collections_abc.py'>, 'genericpath': <module 'genericpath' from 'D:\\Program Files\\Python 3.8.2\\lib\\genericpath.py'>, 'ntpath': <module 'ntpath' from 'D:\\Program Files\\Python 3.8.2\\lib\\ntpath.py'>, 'os.path': <module 'ntpath' from 'D:\\Program Files\\Python 3.8.2\\lib\\ntpath.py'>, 'os': <module 'os' from 'D:\\Program Files\\Python 3.8.2\\lib\\os.py'>, '_sitebuiltins': <module '_sitebuiltins' from 'D:\\Program Files\\Python 3.8.2\\lib\\_sitebuiltins.py'>, 'site': <module 'site' from 'D:\\Program Files\\Python 3.8.2\\lib\\site.py'>}
"""
sys.platform
import sys
print ( sys. platform)
"""
win32
"""
System
platform value
Linux (2.x and 3.x)
‘linux2’
Windows
‘win32’
Windows/Cygwin
‘cygwin’
Mac OS X
‘darwin’
OS/2
‘os2’
OS/2 EMX
‘os2emx’
sys.path
import sys
print ( sys. path)
"""
['E:\\Nextcloud\\NAS\\Study\\Study-Note\\Python\\Pycharm\\Lab\\week5', 'E:\\Nextcloud\\NAS\\Study\\Study-Note\\Python\\Pycharm', 'D:\\Program Files\\Python 3.8.2\\python38.zip', 'D:\\Program Files\\Python 3.8.2\\DLLs', 'D:\\Program Files\\Python 3.8.2\\lib', 'D:\\Program Files\\Python 3.8.2', 'D:\\Program Files\\Python 3.8.2\\lib\\site-packages']
"""
sys.path_hooks
import sys
print ( sys. path_hooks)
"""
[<class 'zipimport.zipimporter'>, <function FileFinder.path_hook.<locals>.path_hook_for_FileFinder at 0x00B2E4A8>]
"""
sys.path_importer_cache
import sys
print ( sys. path_importer_cache)
"""
{'E:\\Nextcloud\\NAS\\Study\\Study-Note\\Python\\Pycharm': FileFinder('E:\\Nextcloud\\NAS\\Study\\Study-Note\\Python\\Pycharm'), 'D:\\Program Files\\Python 3.8.2\\python38.zip': None, 'D:\\Program Files\\Python 3.8.2\\DLLs': FileFinder('D:\\Program Files\\Python 3.8.2\\DLLs'), 'D:\\Program Files\\Python 3.8.2\\lib': FileFinder('D:\\Program Files\\Python 3.8.2\\lib'), 'D:\\Program Files\\Python 3.8.2\\lib\\encodings': FileFinder('D:\\Program Files\\Python 3.8.2\\lib\\encodings'), 'D:\\Program Files\\Python 3.8.2': FileFinder('D:\\Program Files\\Python 3.8.2'), 'D:\\Program Files\\Python 3.8.2\\lib\\site-packages': FileFinder('D:\\Program Files\\Python 3.8.2\\lib\\site-packages'), 'E:/Nextcloud/NAS/Study/Study-Note/Python/Pycharm/Lab/week5/test.py': None}
"""
sys.prefix
Windows
import sys
print ( sys. prefix)
"""
D:\Program Files\Python 3.8.2
"""
Linux
[ root@Private ~ ]
Python 3.8 .2 ( default, Apr 19 2020 , 10 : 20 : 42 )
[ GCC 4.8 .5 20150623 ( Red Hat 4.8 .5 - 39 ) ] on linux
Type "help" , "copyright" , "credits" or "license" for more information.
>> > import sys
>> > print ( sys. prefix)
/ usr/ local
>> >
sys.ps1 / sys.ps2
>> > import sys
>> > print ( sys. ps1)
>> >
>> > print ( sys. ps2)
. . .
>> >
sys.setcheckinterval()
import sys
print ( sys. getcheckinterval( ) )
sys. setcheckinterval( 50 )
print ( sys. getcheckinterval( ) )
"""
100
50
"""
sys.version_info
import sys
print ( sys. version_info)
"""
sys.version_info(major=3, minor=8, micro=2, releaselevel='final', serial=0)
"""