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

How to use Python system module sys

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you the "Python system module sys how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Python system module sys how to use" this article.

1. Output system platform and version

In many Python libraries, the system version is identified using the sys module. Let's take a look at the specific usage code:

Windows system

> > import sys > sys.platform'win32'

Linux system

> import sys > sys.platform'linux'2. Python search module path

When using multiple versions of Python for development, if you find that a module cannot be searched, you can use the sys module to view the search path of the current Python version, as shown below:

> import sys > sys.path ['','/ root/.pyenv/versions/3.7.3/lib/python37.zip','/ root/.pyenv/versions/3.7.3/lib/python3.7','/ root/.pyenv/versions/3.7.3/lib/python3.7/lib-dynload','/ root/.pyenv/versions/ansible/lib/python3.7/site-packages' '/ root/.pyenv/versions/ansible/lib/python3.7/site-packages/cryptography-2.6.1-py3.7-linux-x86_64.egg',' / root/.pyenv/versions/ansible/lib/python3.7/site-packages/paramiko-2.4.2-py3.7.egg',....'/ root/.pyenv/versions/ansible/lib/python3.7/site-packages/pycparser-2.19-py3.7.egg' '/ root/.pyenv/versions/3.7.3/envs/ansible/lib/python3.7/site-packages']

Because the intermediate output is relatively long, some of it is omitted here. Sys.path this variable will output your current Python search path, that is, where Python will find your module. If you find that the path information is inconsistent with your Python version information, it means that you are using the wrong environment configuration. At this time, you need to modify the path of the Python executable file.

3. View a list of loaded modules in the current Python process

Similar to the function above, if the module is not found when the Python script is executed, it depends on whether the module loading path is correct. If the module loading path is correct, you need to check whether the module has been loaded. At this time, you can also use the sys module to troubleshoot, see the following code:

> sys.modules {'sys':,' builtins':,.... 'rlcompleter':}

By default, sys.modules outputs all modules loaded by the current python process and returns the results in a dictionary. The key of the dictionary is the module name, and the value of the dictionary is the path of the module. So if you want to find a module, you can use a dictionary to get the value:

> sys.modules ['rlcompleter']

In the above example, we have found the corresponding path of the rlcompleter module through the dictionary. If you need to check whether a module is loaded correctly and output the result of sys.modules ['your_module_name'] in the project, you will see it clearly.

4. View the details of the exception

In a normal Python course, when it comes to exceptions, you usually use the following format to output the exception content:

> try:... Raise IndexError ('index error'). Except Exception as e:... Print (e)... Index error

But if we use the sys module, we can also see more details, as shown below:

> > import traceback > try:... Raise IndexError ('index error'). Except:... Print (sys.exc_info ())... Traceback.print_tb (sys.exc_info () [2]). (, IndexError ('index error'),) File "", line 2, in

The function sys.exc_info () returns a tuple of three objects, the first is the exception type, the second is the details of the exception, and the third is the memory address of the exception. According to the first two parameters, you can output specific error messages, and the third parameter can output the error in which line with the help of the tracking module traceback.

5. Other tools for sys module

Sys.argv, which saves the command line parameters of the python script. Examples are as follows:

Import sysprint (sys.argv)

The above content is in the script file test.py, and then we execute the command python test.py a b c d, and the output is as follows:

['test.py',' await, 'baked,' crested,'d']

The first element is the script file name, followed by the parameter name. According to this option, you can write python scripts with parameter functions.

Sys.exit exits the program, and you can use this function to end the running program while the program is running. The sample code is as follows:

Import sys

I = 0

While True:

If I < 5:

Print (I)

ITunes 1

Else:

Sys.exit (1)

In the above script, we set the final end code to be 1, so let's test it in linux: ```shell# echo $? "python test.py 0123 echo $? 1

In the above shell code, we can see that before executing the script, $? The value of is 0, and after executing the program, $? The corresponding value is 1. In other words, the program ends abnormally. Then we can use the sys.exit () function to set different closing code to determine the state of the program in the python script used in the production environment.

The above is all the contents of the article "how to use sys in Python system Module". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Development

Wechat

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

12
Report