>首页> IT >

Python基础学习之标准库sys总结

时间:2022-03-18 17:46:26       来源:转载
本篇文章给大家带来了关于python的相关知识,其中主要介绍了标准库sys的相关问题,Python的sys模块提供访问由解释器使用或维护的变量的接口,并提供了一些函数用来和解释器进行交互,操控Python的运行时环境,希望对大家有帮助。

推荐学习:python学习教程

sys作用

>>> import sys

常用变量

sys.version

>>> import sys>>> sys.version3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)]

sys.maxsize

>>> import sys>>> sys.maxsize9223372036854775807

sys.maxunicode

>>> import sys>>> sys.maxunicode1114111

sys.path

当前脚本的path环境变量,如果没有python就找到

>>> import sys>>> sys.path["", "D:\\Python3.8\\python38.zip", "D:\\Python3.8\\DLLs", "D:\\Python3.8\\lib", "D:\\Python3.8", "D:\\Python3.8\\lib\\site-packages"]

sys.platform

系统返回值
Windows‘win32’
Linux‘linux’
Mac‘darwin’
>>> import sys>>> sys.platformwin32

sys.argv

import sysprint(sys.argv)
E:\Python> python 2.py hello python["2.py", "hello", "python"]

sys.executable

>>> import sys>>> sys.executable"D:\\Python3.8\\python.exe"

sys.byteorder

>>> import sys>>> sys.byteorder"little"

sys.version_info

>>> import sys>>> sys.version_infosys.version_info(major=3, minor=7, micro=3, releaselevel="final", serial=0)

sys.api_version

>>> import sys>>> sys.api_version1013

sys.stdin/sys.stdout/sys.stderr

>>> import sys>>> sys.stdin<_io.TextIOWrapper name="" mode="r" encoding="utf-8">>>> sys.stdout<_io.TextIOWrapper name="" mode="w" encoding="utf-8">>>> sys.stderr<_io.TextIOWrapper name="" mode="w" encoding="utf-8">

常用方法

sys.exit()

import sysprint(sys.exit(0))print(sys.exit(1))

sys.modules

>>> import sys>>> sys.modules{"sys": , "builtins": , "_frozen_importlib": , "_imp": , "_warnings": , "_frozen_importlib_external": , "_io": , "marshal": , "nt": , "_thread": , "_weakref": , "winreg": , "time": , "zipimport": , "_codecs": , "codecs": , "encodings.aliases": , "encodings": , "encodings.utf_8": , "_codecs_cn": , "_multibytecodec": , "encodings.gbk": , "_signal": , "__main__": , "encodings.latin_1": , "_abc": , "abc": , "io": , "_stat": , "stat": , "_collections_abc": , "genericpath": , "ntpath": , "os.path": , "os": , "_sitebuiltins": , "_locale": , "_bootlocale": , "types": , "importlib._bootstrap": , "importlib._bootstrap_external": , "warnings": , "importlib": , "importlib.machinery": , "importlib.abc": , "_operator": , "operator": , "keyword": , "_heapq": , "heapq": , "itertools": , "reprlib": , "_collections": , "collections": , "_functools": , "functools": , "contextlib": , "importlib.util": , "mpl_toolkits": , "site": , "atexit": }

sys.modules.keys()

>>> import sys>>> sys.modules.keys()dict_keys(["sys", "builtins", "_frozen_importlib", "_imp", "_warnings", "_frozen_importlib_external", "_io", "marshal", "nt", "_thread", "_weakref", "winreg", "time", "zipimport", "_codecs", "codecs", "encodings.aliases", "encodings", "encodings.utf_8", "_codecs_cn", "_multibytecodec", "encodings.gbk", "_signal", "__main__", "encodings.latin_1", "_abc", "abc", "io", "_stat", "stat", "_collections_abc", "genericpath", "ntpath", "os.path", "os", "_sitebuiltins", "_locale", "_bootlocale", "types", "importlib._bootstrap", "importlib._bootstrap_external", "warnings", "importlib", "importlib.machinery", "importlib.abc", "_operator", "operator", "keyword", "_heapq", "heapq", "itertools", "reprlib", "_collections", "collections", "_functools", "functools", "contextlib", "importlib.util", "mpl_toolkits", "site", "atexit"])

sys.getdefaultencoding()

>>> import sys>>> sys.getdefaultencoding()"utf-8"

sys.getfilesystemencoding()

>>> import sys>>> sys.getfilesystemencoding()utf-8

sys.getrecursionlimit()

>>> import sys>>> sys.getrecursionlimit()  # 查看当前解释器的最大递归深度1000

sys.setrecursionlimit(num)

>>> import sys>>> sys.setrecursionlimit(1100)  # 将解释器的最大递归深度设置为1100>>> sys.getrecursionlimit()  # 再次查看当前解释器的最大递归深度1100

sys.getsizeof()

>>> import sys>>> for obj in [int(), float(), list(), tuple(), set(), dict(), object]:...     print(str(obj.__class__).ljust(20), sys.getsizeof(obj))...        24      24       56      40        216       232       416

sys.getrefcount(obj)

>>> import sys>>> a = [1,2,3]>>> b = a>>> c = b>>> sys.getrefcount(a)4

sys.exc_info()

获取当前正在处理的异常类,exc_type、exc_value、exc_traceback当前处理的异常详细信息

>>> import sys>>> sys.exc_info()(None, None, None)

sys.getwindowsversion()

>>> import sys>>> sys.getwindowsversion()sys.getwindowsversion(major=10, minor=0, build=19041, platform=2, service_pack="")

sys.stdin.readline()

sys.stdout.write()

>>> import sys>>> sys.stdout.write("hello world")hello world11

推荐学习:python教程

以上就是Python基础学习之标准库sys总结的详细内容,更多请关注php中文网其它相关文章!

关键词: 相关文章 常用方法 环境变量