Appearance
Modules
In the development of computer programs, as the amount of program code increases, the code within a single file becomes longer and harder to maintain.
To write maintainable code, we group many functions together and place them in different files. This way, each file contains relatively less code, and many programming languages adopt this method of organizing code. In Python, a .py
file is called a module.
What are the benefits of using modules?
The biggest advantage is that it significantly improves the maintainability of the code. Additionally, you don't have to start coding from scratch. Once a module is written, it can be referenced from other places. When writing programs, we often reference other modules, including Python's built-in modules and third-party modules.
Using modules also helps avoid conflicts between function names and variable names. Functions and variables with the same name can exist in different modules, so when writing your own modules, you don't have to worry about name conflicts with other modules. However, you should still be careful not to conflict with built-in function names. Click here to view all of Python's built-in functions.
You might also think about what to do if different people create modules with the same name. To avoid module name conflicts, Python introduces a method of organizing modules by directories, called packages.
For example, a file named abc.py
is a module named abc
, and a file named xyz.py
is a module named xyz
.
Now, suppose our modules abc
and xyz
conflict with other modules. We can organize these modules using packages to avoid conflicts. The method is to choose a top-level package name, such as mycompany
, and store them in the following directory structure:
mycompany
├─ __init__.py
├─ abc.py
└─ xyz.py
After introducing a package, as long as the top-level package name does not conflict with others, all modules within will not conflict with others. Now, the module name abc.py
becomes mycompany.abc
, and similarly, the module name xyz.py
becomes mycompany.xyz
.
Please note: Every package directory must contain an __init__.py
file. If this file does not exist, Python treats the directory as a regular directory rather than a package. The __init__.py
file can be empty or contain Python code because __init__.py
itself is a module, and its module name is mycompany
.
Similarly, you can have multi-level directories to form a hierarchical package structure. For example, the following directory structure:
mycompany
├─ web
│ ├─ __init__.py
│ ├─ utils.py
│ └─ www.py
├─ __init__.py
├─ abc.py
└─ utils.py
The module name of www.py
is mycompany.web.www
, and the two utils.py
files have module names mycompany.utils
and mycompany.web.utils
, respectively.
Special
When creating your own modules, pay attention to naming to avoid conflicts with Python's built-in module names. For example, Python has a built-in sys
module, so your module should not be named sys.py
; otherwise, you won't be able to import the built-in sys
module.
mycompany.web
is also a module. Please indicate the corresponding .py
file for this module.
Summary
A module is a collection of Python code that can be used by other modules or can use other modules.
When creating your own modules, keep in mind:
- Module names should follow Python's variable naming conventions. Do not use Chinese characters or special characters.
- Module names should not conflict with system module names. It's best to first check if the system already has the module by executing
import abc
in the Python interactive environment. If it succeeds, the system has this module.