Import sys module

First, we enter the SYS module using an import statement. Basically, this statement tells Python that we want to use this module. The SYS module contains functions related to the Python interpreter and its environment.

When Python executes an import sys statement, it looks for the sys.py module in the directory listed in the sys.path variable. If the file is found, the statements in the module’s main block will be run, and the module will be available to you. Note that the initialization process takes place only when we first enter the module. Also, “sys” is short for “system”.

Sys has so many module functions that I can only list a few that I think are useful. Using jack Ma’s saying to find employees, “find the most suitable rather than the most talented”, I personally think I can adapt to it in many aspects, and it is no problem to learn. Sys modules do have a lot of features, but we should focus on those features that are most suitable for us. For this reason, the functions I have listed are those That I think are more suitable for my future development.

(1)sys.argv

A lot of people think, how do I pass parameters outside of my program?This one, you can do it. Such as:

Tesy.py

Import sys

Print sys.argv[number]

In general, the number 0 is the name of the script, 1,2… Is the argument passed under the command line. Such as:

Test.py script content:

import sys

 

print sys.argv[0]

print sys.argv[1]

print sys.argv[2]

print sys.argv[3]

then

[root@databak scripts]# python test.py arg1 arg2 arg3

test.py

arg1

arg2

arg3

See, what’s the corresponding relationship?

The argv variable in the SYS module is indicated by the use of a dot — sys.argv — one advantage of this method is that the name does not conflict with any argv variables used in your program. In addition, it makes it clear that the name is part of the SYS module. The

sys.argv variable is a list of strings (the list is explained in more detail in a later section). In particular, sys.argv contains a list of command-line arguments that are passed to your program using the command line. Here, when we execute python using_sys.py we are arguments, we use the python command to run the using_sys.py module, which is then passed to the program as arguments. Python stores it for us in the sys.argv variable. Remember that the name of the script is always the first argument in the list of sys.argv. So here, ‘using_sys.py’ is sys.argv[0], ‘we’ is sys.argv[1], ‘are’ is sys.argv[2], and ‘arguments’ is sys.argv[3]. Note that Python counts from zero, not one.

(2)sys.platform

As you all know, today’s programs are more popular across platforms. Simply put, the program can run on Windows, Linux or without modification, and it sounds great. So that’s where this function comes in handy.

Let’s say we want to implement a clear terminal, clear for Linux and CLS for Windows

Ostype=sys.platform()

If the or ostype ostype = = “Linux” = = “linux2” :

Cmd = “clear”

Else:

Cmd = “CLS”

(3) sys. Exit (n)

At the end of the main program, the interpreter exits automatically, but if you need to exit the program halfway, you can call the sys.exit function, which returns an optional integer argument to the calling program. This means that you can capture the call to sys.exit from the main program. (Note: 0 is normal exit, others are abnormal, abnormal events can be thrown for capture!)

Sys. exit from the Python program will raise a systemExit exception, which you can do to clear out. The default normal exit status of this optional parameter is 0, and the range of numerical parameters is 0-127. There’s another type, which is the strings object type that’s shown here.

(4)sys.path

You all know something about modules, right?Do you need to import one of the modules before you can use it?The answer is yes. A: Then import, import__import__ the order need not be carried. So what happens inside Python when you execute import module_name?Simply put, search for module_name. Search module.name based on the path of sys.path

> > > sys.path

[‘ ‘, ‘/ usr/local/lib/python24. Zip’, ‘/ usr/local/lib/python2.4’, ‘/ usr/local/lib/python2.4/platt – freebsd4’, ‘/ usr/local/lib/python2.4/lib – tk’, ‘/ usr/local/lib/python2.4/lib – dynload’, ‘/ usr/local/lib/python2.4/site – packages’]

You later write the module can be put above a directory, you can search correctly. You can also add your own module path. Sys. Path. Append (” mime the module path “).

Sys. path contains a list of directory names for input modules. You can observe that the first string of sys.path is empty — this empty string indicates that the current directory is also part of sys.path, which is the same as the PYTHONPATH environment variable. This means that you can directly enter the module located in the current directory. Otherwise, you will have to place your modules in one of the directories listed on sys.path. First, we enter the SYS module using an import statement. Basically, this statement tells Python that we want to use this module. The SYS module contains functions related to the Python interpreter and its environment.

(5)sys.modules

This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks.

Python.org
That’s pretty clear in the manual.

For names in sys.modules.keys():

If names ! = “sys” :

(6)sys.stdin,sys.stdout,sys.stderr

Stdin, stdout, and stderr variable contains corresponding with the standard I/O flow stream objects. If you need to better control the output, and the print can’t meet your request, they may be just what you need. You’ll also be able to replace them, then you can redirect output and input to the other devices (device), or in a standard way of dealing with them

When Python executes an import sys statement, it looks for the sys.py module in the directory listed in the sys.path variable. If the file is found, the statements in the module’s main block will be run, and the module will be available to you. Note that the initialization process takes place only when we first enter the module. Also, “sys” is short for “system”.

sys module argv variables are indicated by using the dot notation — sys. Argv — one advantage of this method is that the name does not conflict with any argv variables used in your program. In addition, it makes it clear that the name is part of the SYS module. The

sys.argv variable is a list of strings (the list will be explained in more detail in a later section). In particular, sys.argv contains a list of command-line arguments that are passed to your program using the command line.

if you are using an IDE to write and run these programs, look in the menu for a way to specify the command line parameters of the program. Here, when we execute python using_sys.py we are arguments, we use the python command to run the using_sys.py module, which is then passed to the program as arguments. Python stores it for us in the sys.argv variable.

remember that the name of the script is always the first argument in the list of sys.argv. So here, ‘using_sys.py’ is sys.argv[0], ‘we’ is sys.argv[1], ‘are’ is sys.argv[2], and ‘arguments’ is sys.argv[3]. Note that Python counts from zero, not one.

sys.path contains a list of directory names for the input modules. You can observe that the first string of sys.path is empty — this empty string indicates that the current directory is also part of sys.path, which is the same as the PYTHONPATH environment variable. This means that you can directly enter the module located in the current directory. Otherwise, you will have to place your modules in one of the directories listed on sys.path.

Read More: