Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sys module of python standard library module

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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)]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report