Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python sys Module

Python sys Module

sys is a module in the Python standard library that provides functions for interacting with the Python interpreter and its environment.

Pass sys With the library you can access variables and functions related to the Python interpreter, such as command-line arguments, standard input/output, and program exit.

Import the sys library

When using sys Before using the library you need to import it first, as follows:


import sys

Example

import

sys

List all attributes and methods of the os module

print

(

dir

(

os

)

)


Common features of the sys library

1. Command-line arguments

sys.argv is a list containing the command-line arguments.sys.argv[0] is the name of the script; the following elements are the arguments passed to the script.

Example code:

Example

import

sys

print

(

"Script name:"

,

sys

.

argv

[

0

]

)

print

(

"Argument list:"

,

sys

.

argv

[

1

:

]

)

How to run:


python script.py arg1 arg2

Output result:


脚本名称: script.py
参数列表: ['arg1', 'arg2']

2. Program exit

sys.exit() is used to exit the program. You can pass an integer as the exit status code; usually 0 means success and a non-zero value means an error.

Example code:

Example

import

sys

print

(

"Program starts"

)

sys

.

exit

(

0

)

print

(

"This line will not be executed"

)

3. Standard input and output

sys.stdin、sys.stdout and sys.stderr represent the standard input, standard output and standard error streams respectively. You can redirect these streams to implement custom input/output behavior.

Example code:

Example

import

sys

Redirect standard output to a file

with

open

(

'output.txt'

,

'w'

)

as

f:

   

sys

.

stdout

=

f

   

print

(

"This line will be written to output.txt"

)

Restore standard output

sys

.

stdout

=

sys

.stdout

print

(

"This line will be displayed on the console"

)

4. Python version information

sys.version and sys.version_info provides version information about the current Python interpreter.

Example code:

Example

import

sys

print

(

"Python version:"

,

sys

.

version

)

print

(

"Version information:"

,

sys

.

version_info

)

Output result:

Python 版本: 3.9.7 (default, Aug 31 2021, 13:28:12) 
[GCC 7.5.0]
版本信息: sys.version_info(major=3, minor=9, micro=7, releaselevel='final', serial=0)

5. Module search path

sys.path is a list containing the paths the Python interpreter searches when importing modules. You can modify this list to add custom module search paths.

Example code:

Example

import

sys

print

(

"Module search path:"

,

sys

.

path

)

sys

.

path

.

append

(

'/custom/path'

)

print

(

"Updated module search path:"

,

sys

.

path

)


Common sys module attributes

property Description
sys.argv Command-line argument list;sys.argv[0] is the script name
sys.path Python module search path (PYTHONPATH)
sys.modules Dictionary of loaded modules
sys.platform Operating system platform identifier (such as 'win32', 'linux', 'darwin')
sys.version Python interpreter version information
sys.executable Absolute path of the Python interpreter
sys.stdin Standard input stream (file object)
sys.stdout Standard output stream (file object)
sys.stderr Standard error stream (file object)
sys.byteorder Byte order ('little' or 'big')
sys.maxsize Maximum integer value (2**31-1 or 2**63-1)

Common sys module methods

Method Description
sys.exit([status]) Exit the program;status=0 means a normal exit
sys.getsizeof(obj) Return the number of bytes of memory occupied by an object
sys.getdefaultencoding() Get the default string encoding (usually 'utf-8')
sys.setrecursionlimit(limit) Set the recursion depth limit (default 1000)
sys.getrecursionlimit() Get the current recursion depth limit
sys.getrefcount(obj) Return the reference count of an object
sys.exc_info() Get the current exception information ((type, value, traceback))
sys.settrace(tracefunc) Set the debug trace function
sys.setprofile(profilefunc) Set the profiling function