Python Common Module--sys module

Source: Internet
Author: User
Tags exit in

SYS module has many functions, the following describes a few common modules.

1.SYS.ARGV: Passing parameters from outside to inside the program

# !/usr/bin/env python Import SYS Print (Sys.argv[0])  # Argv[0] Indicates the name of the function Print (Sys.argv[1])  # first parameter Print (Sys.argv[2])   # second parameter ~                   

Execution Result:

[[Email protected] script] # python sys_argv.py frist_argv second_argv SYS_ARGV.PYFRIST_ARGVSECOND_ARGV

2.sys.exit (n):

Execution to the end of the main program, the interpreter automatically exits, but if you need to exit halfway, you can call the Sys.exit () function, with an optional integer parameter returned to the program that called it, indicating that you can capture the call to Sys.exit in the main program. (0 is normal exit, the rest is an exception.) )

3.sys.path ():

You can get a collection of strings for the specified module search path, and you can place the well-written module under a path that can be found when the program is import.

Import sys>>> sys.path['/usr/local/python3.6/lib/python36.zip ' ' /usr/local/python3.6/lib/python3.6 ' ' /usr/local/python3.6/lib/python3.6/lib-dynload ' ' /usr/local/python3.6/lib/python3.6/site-packages ']

4.sys.modules ():

Sys.modules () is a global dictionary that is loaded in memory after the python is started. Whenever a programmer imports a new module, Sys.modules will automatically record the module. When the module is imported again the second time, Python looks directly from the dictionary to speed up the program. It has all the methods that the dictionary has.

# !/usr/bin/env python Import SYS Print (Sys.modules.keys ()) Print (Sys.modules.values ()) Print (sys.modules['os')

5. sys.platform(): get the current system platform

>>> sys.platform'linux'

6.sys.version (): Get version information for Python interpreter

>>> sys.version'3.6.3 (default, OCT, 18:55:29) \n[gcc 4.8.5 20150623 (Red Hat 4.8.5 -11)]'

Python Common Module--sys module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.