I. Definition of the module
definition
The Python module, which is a python file that ends with a. Py, contains Python object definitions and Python statements. Modules allow you to logically organize your Python code snippets and assign related code to a module to make your code better and easier to understand. Modules can define functions, classes and variables, and modules can contain executable code.
Purpose
Improved reusability
usage
1) Import Module name
Note: If there is a class in the module name, you must instantiate the class to invoke the method in the class: module name. Class name (). Method
2) from the module name import corresponding class or function (usually called the method inside the class)
two. Import the module
Recommended Python code style:
♦ Import packages and modules using import x
♦ Use from x import Y to import the module, where x is the package name and Y is the module name without the package name
♦ Using from x import y as Z, if two modules to be imported are called Y or y is too long
♦ Use from x.y import Z imports module Z to use the full path of the package (Y is the sub-package under X packet), do not nest, after import to ensure that only the module, without package name
Not recommended import method (full Import): From math import *
Packages and folders
For better organization of modules, use packages to manage
The package is a folder, but the __init__.py file must exist under the folder
In the simplest case, you only need an empty __init__.py file to
New Python Package
The __init__.py file is automatically generated under the package
three. Relative path import
Python3, import the other Python files under the same project, using relative paths (the root directory is the project directory)
Project when you create a project, for the root directory
module in the root directory: Import X
module in a folder under the root directory: from Y import X
Example 1
The module name is in the root directory of the project
# Introducing Modules Import Test # Introduction Class from Import TTT
Example 2
module in a package in the root directory of the project
# Introducing Modules from Import Test # Introduction Class from Import TTT
References to modules in Python