Knowledge Content:
1. Definition and classification of modules
2. Import of modules
3. Modules and Packages
I. Definition and classification of modules
1. What is a module
A module is a collection of code that implements a function, which is made up of a whole bunch of code.
Like functional programming and process-oriented programming, functional programming accomplishes a function, and other code is used to invoke it, providing reusability of code and coupling between the code. For a complex function, multiple functions may be required to complete (functions can be in different. py files), and the Code collection consisting of N. py files is called a module.
such as: OS is a system-related module; Re is a module that deals with regular expression-related
2. Classification of modules
Modules can be divided into three types:
- Custom Modules
- Built-in standard module (also known as standard library)
- Open source Module
Reference http://www.cnblogs.com/wupeiqi/articles/4963027.html for the use of custom modules and open source modules
Second, the module import
1.3 Ways to import modules:
- Import module name [as Alias] Use this method after importing a module if you want to use an object in a module, you can use the following methods: module name (alias). Object Name
- From module name import object name [as Alias]
- Import all objects in a module: from Module name import *
Note: It is not recommended to use the last
2. Order of import Modules
(1) Import the module in Python standard library (Python environment comes with module), for example Os\sys\re\math
(2) Importing third-party library modules, such as Numpy\scrapy
(3) Import local modules that you define or develop
Example:
1#__author__ = "wyb"2#Date:2018/3/934Import OS#Importing Python's own module OS5import sys # Import python with module Sys 6 import requests # import python 3rd party module Requests 7 # Import Package # imports your own Write module package 8 9 from bs4 import beautifulsoup # Import BeautifulSoup module from BS4 package 10 from math import sqrt # import sqrt functions from the Math module
Three, module and package
1. Benefits of using Modules
- Greatly improve the maintainability of your code
- Improve Code reusability
Note:
The use of modules also avoids conflicting function names and variable names. Functions and variables of the same name can exist in different modules individually, so we do not have to think about names that conflict with other modules when we write our own modules. But also be aware that try not to conflict with the built-in function name.
2. What is a package
You may also think, what if different people write the same module name? To avoid module name collisions, Python introduces a way to organize modules by directory, called packages, as follows:
Note: Each package directory will have a __init__.py
file, this file must exist, otherwise, Python will treat this directory as a normal directory, rather than a package.
__init__.py
It can be an empty file, or it can have Python code, because it __init__.py
is a module in itself.
When you create a module, you should be careful about naming it and not conflict with the module name that comes with Python. For example, the system comes with a sys
module, its own module is not named sys.py
, otherwise it will not be able to import the system's own sys
module
Python Module 1 Module Introduction