During the development of a computer program, as more and more code is written, the code in a single file becomes increasingly lengthy and difficult to maintain.
To write maintainable code, we group many functions and place them in different files. This way, each file contains a relatively small amount of code, and many programming languages adopt this approach to organizing code. In Python, a .py file is called a Module.
What are the benefits of using modules?
The biggest advantage is that it greatly improves the maintainability of the code. Secondly, you don’t have to write code from scratch. Once a module is written, it can be referenced elsewhere. When we write programs, we also often reference other modules, including Python’s built-in modules and third-party modules.
Using modules also avoids conflicts between function names and variable names. Functions and variables with the same name can exist in different modules completely. Therefore, when writing our own modules, we don’t have to worry about name conflicts with other modules. However, we should also be careful to avoid conflicts with built-in function names as much as possible. Click here to view all built-in Python functions.
You may also wonder: what if different people write modules with the same name? To avoid module name conflicts, Python introduces a method of organizing modules by directories, called a Package.
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 two modules abc and xyz have name conflicts with other modules. We can then organize the modules through a package to avoid conflicts. The method is to choose a top-level package name, such as mycompany, and store the files in the following directory structure:
mycompany
├─ __init__.py
├─ abc.py
└─ xyz.py
After introducing the package, as long as the top-level package name does not conflict with others, all modules under it will not conflict with others. Now, the name of the abc.py module becomes mycompany.abc, and similarly, the name of the xyz.py module becomes mycompany.xyz.
Please note that there must be an init.py file in each package directory. This file is mandatory; otherwise, Python will treat the directory as an ordinary 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, multi-level directories can be used to form a multi-level 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 the file www.py is mycompany.web.www, and the module names of the two utils.py files are mycompany.utils and mycompany.web.utils respectively.
When creating your own modules, pay attention to naming—do not conflict with the names of Python’s built-in modules. For example, the system has a built-in sys module, so your own module cannot be named sys.py; otherwise, you will not be able to import the system’s built-in sys module.
mycompany.web is also a module; please point out the .py file corresponding to this module.
A module is a collection of Python code that can use other modules and be used by other modules.
When creating your own modules, note the following:
import abc in the Python interactive environment—if it succeeds, it means the system has this module.