前言
此模块提供一种可以访问需要使用的或者被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)
"""