SoFunction
Updated on 2024-11-13

Do you know about sys in the Python standard library?

Role of sys

Python's sys module provides an interface to access variables used or maintained by the interpreter, and provides functions to interact with the interpreter and manipulate Python's runtime environment.

>>> import sys

Common Variables

Returns the version number of the Python interpreter; used when a program needs to be run with the specified version number.

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

Indicates the maximum int value carried by the operating system

>>> import sys
>>> 
9223372036854775807

The integer that gives the maximum Unicode code point value, i.e. 1114111 (hex 0x10FFFF).

>>> import sys
>>> 
1114111

The path environment variable for the current script, if it doesn't have python find the

>>> import sys
>>> 
['', 'D:\\Python3.8\\', 'D:\\Python3.8\\DLLs', 'D:\\Python3.8\\lib', 'D:\\Python3.8', 'D:\\Python3.8\\lib\\site-packages']

Returns the operating system platform name, useful when writing cross-platform applications

systems return value
Windows ‘win32’
Linux ‘linux’
Mac ‘darwin’
>>> import sys
>>> 
win32

Take the script name of a python script runtime and the parameters as a list and output it. Implement passing parameters from outside the program like program content

import sys

print()
E:\Python> python  hello python
['', 'hello', 'python']

A string giving the absolute path to the Python interpreter's executable binary. If Python cannot retrieve the true path to its executable, it will be the empty string or None.

>>> import sys
>>> 
'D:\\Python3.8\\'

Indicator of local byte order - the value is 'big' on big endian (highest significant bit first) operating systems, and 'little' on little endian (lowest significant bit first) operating systems.

>>> import sys
>>> 
'little'

sys.version_info

A tuple of five components containing version numbers: major, minor, micro, releaselevel, and serial.

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

sys.api_version

The C API version of this interpreter.

>>> import sys
>>> sys.api_version
1013

//

Standard inputs, outputs, errors.

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

Common methods

()

Exit the program, exit(0) for normal exit

import sys

print((0))
print((1))

Returns the module fields imported by the system, key is the module name, value is the module

>>> import sys
>>> 
{'sys': <module 'sys' (built-in)>, 'builtins': <module 'builtins' (built-in)>, '_frozen_importlib': <module 'importlib._bootstrap' (frozen)>, '_imp': <module '_imp' (built-in)>, '_warnings': <module '_warnings' (built-in)>, '_frozen_importlib_external': <module 'importlib._bootstrap_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:\\Python3.8\\lib\\'>, '': <module '' from 'D:\\Python3.8\\lib\\encodings\\'>, 'encodings': <module 'encodings' from 'D:\\Python3.8\\lib\\encodings\\__init__.py'>, 'encodings.utf_8': <module 'encodings.utf_8' from 'D:\\Python3.8\\lib\\encodings\\utf_8.py'>, '_codecs_cn': <module '_codecs_cn' (built-in)>, '_multibytecodec': <module '_multibytecodec' (built-in)>, '': <module '' from 'D:\\Python3.8\\lib\\encodings\\'>, '_signal': <module '_signal' (built-in)>, '__main__': <module '__main__' (built-in)>, 'encodings.latin_1': <module 'encodings.latin_1' from 'D:\\Python3.8\\lib\\encodings\\latin_1.py'>, '_abc': <module '_abc' (built-in)>, 'abc': <module 'abc' from 'D:\\Python3.8\\lib\\'>, 'io': <module 'io' from 'D:\\Python3.8\\lib\\'>, '_stat': <module '_stat' (built-in)>, 'stat': <module 'stat' from 'D:\\Python3.8\\lib\\'>, '_collections_abc': <module '_collections_abc' from 'D:\\Python3.8\\lib\\_collections_abc.py'>, 'genericpath': <module 'genericpath' from 'D:\\Python3.8\\lib\\'>, 'ntpath': <module 'ntpath' from 'D:\\Python3.8\\lib\\'>, '': <module 'ntpath' from 'D:\\Python3.8\\lib\\'>, 'os': <module 'os' from 'D:\\Python3.8\\lib\\'>, '_sitebuiltins': <module '_sitebuiltins' from 'D:\\Python3.8\\lib\\_sitebuiltins.py'>, '_locale': <module '_locale' (built-in)>, '_bootlocale': <module '_bootlocale' from 'D:\\Python3.8\\lib\\_bootlocale.py'>, 'types': <module 'types' from 'D:\\Python3.8\\lib\\'>, 'importlib._bootstrap': <module 'importlib._bootstrap' (frozen)>, 'importlib._bootstrap_external': <module 'importlib._bootstrap_external' (frozen)>, 'warnings': <module 'warnings' from 'D:\\Python3.8\\lib\\'>, 'importlib': <module 'importlib' from 'D:\\Python3.8\\lib\\importlib\\__init__.py'>, '': <module '' from 'D:\\Python3.8\\lib\\importlib\\'>, '': <module '' from 'D:\\Python3.8\\lib\\importlib\\'>, '_operator': <module '_operator' (built-in)>, 'operator': <module 'operator' from 'D:\\Python3.8\\lib\\'>, 'keyword': <module 'keyword' from 'D:\\Python3.8\\lib\\'>, '_heapq': <module '_heapq' (built-in)>, 'heapq': <module 'heapq' from 'D:\\Python3.8\\lib\\'>, 'itertools': <module 'itertools' (built-in)>, 'reprlib': <module 'reprlib' from 'D:\\Python3.8\\lib\\'>, '_collections': <module '_collections' (built-in)>, 'collections': <module 'collections' from 'D:\\Python3.8\\lib\\collections\\__init__.py'>, '_functools': <module '_functools' (built-in)>, 'functools': <module 'functools' from 'D:\\Python3.8\\lib\\'>, 'contextlib': <module 'contextlib' from 'D:\\Python3.8\\lib\\'>, '': <module '' from 'D:\\Python3.8\\lib\\importlib\\'>, 'mpl_toolkits': <module 'mpl_toolkits' (namespace)>, 'site': <module 'site' from 'D:\\Python3.8\\lib\\'>, 'atexit': <module 'atexit' (built-in)>}

()

Returns a list of all imported module names

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

()

Returns the name of the current default string encoding used by the Unicode implementation.

>>> import sys
>>> ()
'utf-8'

()

Returns the encoding name used to convert between Unicode filenames and byte filenames

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

()

Returns the maximum number of recursions

>>> import sys
>>> ()  # View the maximum recursion depth of the current interpreter
1000

(num)

Setting the maximum number of recursions

>>> import sys
>>> (1100)  # Set the maximum recursion depth of the interpreter to 1100
>>> ()  # Check the maximum recursion depth of the current interpreter again
1100

()

Get the size of the memory occupied by the object (expressed in bytes)

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

(obj)

Returns the reference count of obj. The returned count is usually one higher than expected because it contains (temporary) references as arguments.

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

sys.exc_info()

Get the exception class that is currently being handled, theexc_typeexc_valueexc_tracebackDetails of the currently handled exception

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

()

Get the version of Windows, valid on Windows systems

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

()

Reading a line from standard input reads the line breaks at the end

()

Write to standard output

>>> import sys
>>> ("hello world")
hello world11

summarize

That's all for this post, I hope it was helpful and I hope you'll check back for more from me!