[Python] module import ① (Introduction to Python modules | Module concept | Module function | Module features | Common Python modules | Python module import | Python module import syntax )





1. Introduction to Python modules




1. Module concept


Module, the English name is "Module";

A Python module is a Python code file with a suffix of .py, defined in the module:

  • function
  • kind
  • variable
  • executable code

2. Module function


Python provides many modules, and each module can realize some functions;

Modules can be used as toolkits, and each toolkit encapsulates different tools, such as: the time module encapsulates toolkits related to time operations;


3. Module Features


Python modules can be referenced and reused by other Python programs, which allows code to be organized into a more manageable size and enables increased code reusability;

Python modules support multi-level nesting. A Python module can be included in other Python modules, and can also be included by other Python modules. The module nesting structure can make the code organization more flexible and maintainable;


4. Common Python modules


The standard library module that comes with Python contains code snippets such as functions and classes commonly used in Python; in addition to the standard library, there are also a large number of third-party module library modules;


Common Python modules:

  • time module: Provides time-related functions, including obtaining current time/formatting time/parsing time, etc.;
  • datetime module: provides richer date and time related functions, including date calculation/time difference calculation, etc.;
  • random module: Provides a pseudo-random number generator, which can be used to generate random numbers;
  • os module: Provides functions to operate the file system, including file/directory operations, etc.;
  • sys module: Provides functions to interact with the Python interpreter, including obtaining command line parameters/exiting the Python program, etc.;
  • shutil module: provides file operation functions, including file copy/move/delete, etc.;
  • json module: Provides functions to serialize Python objects into JSON format/deserialize JSON format into Python objects;
  • pickle module: Provides functions to serialize Python objects into binary format/deserialize binary format into Python objects;
  • shelve module: Provides the function of persisting Python objects to disk, which can be used to implement simple databases;
  • xml module: Provides the function of parsing XML documents, which can be used to parse data in XML format;
  • configparser module: Provides the function of parsing INI format configuration files, which can be used to read configuration files;
  • sbuprocess module: Provides the function of running external programs in subprocesses, which can be used to implement functions such as process pool/asynchronous execution of tasks;
  • hashlib module: Provides functions for hash calculation of data, which can be used to calculate checksums of files, etc.;




2. Python module import




1. Python module import syntax


Python module import syntax:

[from 模块名称] import [模块 || 变量 | 函数 | *] [as 别名]

In the above syntax, square brackets []indicate optional content;

from 模块名称may as 别名or may not be written;

import [模块 | 类 | 变量 | 函数 | *]is mandatory;


2. Commonly used module import combinations


Commonly used module import combinations:

  • import 模块名称: import all content in the module, including functions, variables and classes, etc., you can directly use the name in the module to access them;
  • from 模块名称 import 类/变量/方法名称: Import the class/variable/method corresponding to the specified name in the specified module, rather than the entire module. After using this method, you can directly use the specified name to access them without adding the module name before the name;
  • from 模块名称 import 指定类/变量/方法名称 as 别名: On the basis of importing the class/variable/method corresponding to the specified name in the specified module, set an alias for the imported content, and you can directly use the alias to access the imported content;

おすすめ

転載: blog.csdn.net/han1202012/article/details/131386326
おすすめ