Learning python (eighteen)-modules and packages

1. Module

Python  provides powerful module support, which is mainly reflected in the fact that not only the Python standard library contains a large number of modules (called standard modules), but also a large number of third-party modules. Developers can also develop custom modules. Through these powerful modules, developers' development efficiency can be greatly improved. So, what exactly is a module? Module, English is Modules, as for what a module is, it can be summarized in one sentence: a module is a Python program. In other words, any Python program can be used as a module, including all Python programs written in the previous chapters, can be used as a module. Write Python code in a file, but with the complexity of the program, the program volume will continue to grow. In order to facilitate maintenance, it is usually divided into multiple files (modules), which can not only improve the maintainability of the code , Can also improve the reusability of the code. The reusability of the code is reflected in the fact that when a module is written, as long as a certain function in the module (implemented by variables, functions, and classes) is needed in the programming process, there is no need to do repetitive writing work, directly in the program Import this module into the, you can use this function.

I talked about packaging earlier, and also introduced many structures with packaging characteristics, such as:

  • Many containers, such as lists, tuples, strings, dictionaries, etc., are all encapsulation of data;
  • Functions are encapsulation of Python code;
  • Class is the encapsulation of methods and properties, it can also be said that the encapsulation of functions and data.

The module introduced in this section can be understood as a more advanced encapsulation of the code, that is, the code that can achieve a specific function is written in the same .py file and used as an independent module, which is convenient Import and use other programs or scripts, while effectively avoiding conflicts between function names and variable names.

, I have seen the syntax of importing modules using import, but in fact there are more detailed usages of import, mainly in the following two ways:

  1. import 模块名1 [as 别名1], 模块名2 [as 别名2],…: Use the import statement of this syntax format to import all members (including variables, functions, classes, etc.) in the specified module. Not only that, when you need to use a member in a module, you need to use the module name (or alias) as a prefix, otherwise the Python interpreter will report an error.
  2. from 模块名 import 成员名1 [as 别名1],成员名2 [as 别名2],…: Use the import statement of this syntax format, only the members specified in the module will be imported, not all members. At the same time, when the member is used in the program, there is no need to add any prefix, and the member name (or alias) can be used directly.

You can also import multiple modules at once, separated by commas. When importing module members, you can also specify aliases for the members. When form...import imports module members, it supports importing multiple members at once. When importing module members, you can also assign aliases to the members.

Normally, after importing a module using the import statement, Python will look for the specified module file in the following order:

  • Search in the current directory, that is, the directory where the currently executing program file is located;
  • Find in each directory under PYTHONPATH (environment variable);
  • Find it in the default installation directory of Python.

All the directories mentioned above are stored in the sys.path variable of the standard module sys. Through this variable, we can see all the directories that the specified program file supports to find. In other words, if the module to be imported is not stored in the directory displayed by sys.path, then when the module is imported and the program is run, the Python interpreter will throw a ModuleNotFoundError (module not found) exception.

(1) The storage location of the module file can be temporarily added to the module.path variable, that is, add the directory to the module.path; (2) Save the module to the specified location; (3) Set the environment variable

When we import a module to a file, we import variables, functions and classes in the module whose names do not start with an underscore (single underscore "_" or double underscore "__"). Therefore, if we don't want a member in the module file to be imported into other files, we can add an underscore before its name.

In addition, you can also use the __all__ variable provided by the module. The value of the variable is a list that stores the names of some members (variables, functions, or classes) in the current module. By setting the __all__ variable in the module file, when other files import the module in the form of "from module name import *", only members specified in the __all__ list can be used in the file.

2 bags

A package is a folder, but there must be a file named "__init__.py" in the folder. This is a requirement of Python 2.x, and in Python 3.x, __init__.py is not necessary for the package. A package is a folder containing multiple modules, and its essence is still a module, so a package can also contain a package. Compared to modules and packages, libraries are a larger concept. For example, each library in the Python standard library has multiple packages, and each package has several modules.

To view the module members, you can use the dir() function and the __all__ variable.

You can use the help() function to get help information for a specified member (or even the module). View the information of a specific member because the member itself contains a description document that represents its own identity (essentially a string, located at the beginning of the member). Regardless of whether it is a function or a class, you can use the __doc__ attribute to get their documentation, and modules are no exception. The bottom layer of the help() function is also implemented with the help of the __doc__ attribute.

You can find the specific storage location of the module (or package) file through the __file__ attribute, and directly view its source code. By calling the absolute path output by the __file__ attribute, we can easily find the source file of the module (or package). Note that not all modules provide the __file__ attribute, because not all modules are implemented in  Python  language, and some modules use other programming languages ​​(such as C language).

Guess you like

Origin blog.csdn.net/qq_35789421/article/details/113663768