Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Sys module of python standard library module

Shulou Source: shulou.com Published: 2022-06-03 07:34:54 11月01日 Update

The role of the sys module

The sys module is used for interpreter-related operations, and is usually used for passing parameters in scripts

Common usage of sys module

The command line argument list passed by sys.argv to the Python script, the first element being the path of the program itself

Sys.executable returns the absolute path of the python interpreter in the current system

Exit in the middle of sys.exit ([arg]) program. Arg=0 is normal exit.

Sys.platform returns the name of the operating system platform, and Linux is linux2,Windows and win32

Sys.path returns the search path of the module, initializing with the value of the PYTHONPATH environment variable

Sys.version gets the version information of the python interpreter program

Sys.stdout.write (str)

Display and demonstration

Position parameter

Import sys

Print (sys.argv [0])

Print (sys.argv [1])

Print (sys.argv [2])

Print (sys.argv [3])

Print ("hello world")

Execution result

.\ sys module .py # sys.argv [0] script name

A # first parameter

B # second parameter

C # third parameter

Hello world

Exit the program halfway

Import sys

Print (sys.argv [0])

Print (sys.argv [1])

Print (sys.argv [2])

Print (sys.argv [3])

Sys.exit ()

Print ("hello world")

Display of implementation results

.\ sys module .py

A

B

C

PS: the print command after sys is not executed

Get module path

When we use the import import module, we search for the imported module name according to the path of the sys.path

Import sys

For i in sys.path:

Print (I)

After implementation

C:\ python36\ python36.zip

C:\ python36\ DLLs

C:\ python36\ lib

C:\ python36

C:\ python36\ lib\ site-packages

Ps displays the value of the PYTHONPATH environment variable

Get the current system platform

Import sys

Sys.platform

'win32'

Get the path to the Python interpreter

Print (sys.executable)

C:\ python36\ python3.exe

Get the version of the python program

Print (sys.version)

3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)]

Tags: Module path parameter program interpreter interpretation name system script variable command platform version environment result search operating system three location function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Redmi Microsoft Xiaomi macOS