# This is a learning note for the Liaoche teacher Python tutorial
1. Overview
To write maintainable code, we group many functions into separate files so that each file contains relatively few code, and in Python, a. py file is called a module.
1.1 , the benefits of using modules
Improved maintainability of the code
When a module is written, it can be referenced elsewhere
Avoid collisions of function names and variable names. Functions and variables of the same name can exist in separate modules
1.2 , python ways to organize your modules
To avoid module name collisions, Python introduces a way to organize modules by directory, called packages.
The practice of the package is:
Select a top-level package name, such as MyCompany, to be stored in the following directory:
MyCompany
├─ __init__.py
├─ abc.py
└─ xyz.py
Once the package is introduced, all modules will not conflict with others as long as the package name in the top layer does not conflict with others. Now,abc.pythe name of the module becomesMYCOMPANY.ABC"formerly called ABC", like that,xyz.pythe module name becomes themycompany.xyz"formerly calledXYZ"
__init__.py This file must exist, otherwise Python will treat the directory as a normal directory, not a package.
_ _init__.py can be an empty file, or it can have Python code, because __init__.py itself is a module, and its module name is MyCompany
Similarly, there can be multi-level catalogs, which form a multi-level package structure. For example, the following directory structure:
MyCompany
├─Web
│ ├─__init__.py
│ ├─utils.py
│ └─www.py
├─ __init__.py
├─ abc.py
└─ xyz.py
filewww.pythe module name ismycompany.web.www, two filesutils.pythe module names aremycompany.utilsand themycompany.web.utils.
Python Learning Note __5 module