Python Common Module - Module and introduced Introduction

Python Common Module - Module and introduced Introduction

First, what is a module?

During development of a computer program, with the program code you write more and more, in a file, the code will become longer and more difficult to maintain.

In order to write code maintainable, we put a lot of function groups, which are placed in a separate file, so each file contains the code is relatively small, and many programming languages ​​have adopted this form of organization code.

In Python to a .py file can be called a module (Module).

Second, using the module What are the benefits?

1, the biggest benefit is to greatly improve the maintainability of the code. Secondly, you do not have to write code from scratch. When a write module is completed, it can be referenced elsewhere. When we write programs, often reference other modules, including a built-in Python modules and modules from third parties.

2, using the module also avoid function names and variable name conflicts. Each module has a separate namespaces, so functions and variables of the same name can exist in different modules respectively, so we own in the preparation module, regardless of the name conflict with other modules.

Third, the classification module

Module is divided into three types:

1, built-in standard modules (also known as the standard library) execution help ( 'modules') to see a list of all that comes with python modules.

2, a third party open source modules, by pip install 模块名networking installation.

3, the custom module.

Fourth, the module import & calls

import module_a  #导入
from module import xx
from module.xx.xx import xx as rename #导入后重命令
from module.xx.xx import *  #导入一个模块下的所有方法,不建议使用
module_a.xxx  #调用

Note: Once the module is called, which is equivalent to execute another py file code.

Fifth, custom modules

The most simple, create a .py file, you can call module, you can import in another program.

Sixth, the module search path

You will find yourself writing module can only be imported at the current path, the directory for a re-import your own modules on an error saying can not find it, which is why?

This import module search path related to:

import sys
print(sys.path)

Output (Note: The above code is not the same may be outputted in a different computer)

['', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

When you import a module, Python interpreter will turn to follow the above list order matching module down each directory name you want to import, just to match the module name in a directory, import immediately, no longer continue in the future to find.

Note: The first element of the list is empty, that means the current directory, so your own custom module is a priority in the current directory will be imported.

If you want to create our own modules can be called at any place, it would have to make sure you find the module file, at least in the list of modules path.

We generally write their own modules on with a "site-packages" directory words, we downloaded from the Internet to install a variety of third-party modules are generally placed in this directory.

Guess you like

Origin www.cnblogs.com/Kwan-C/p/11620381.html