What can I do after this course?
Through this course, you will learn how to customize your own modules and packages, and how to reference them into your own projects for code reuse.
How long does the course take?
5-10 minutes
Course content
What is the Python standard library?
Python also comes with a set of modules that are called the standard library. To put it simply, the system comes with some convenient methods for us to use, which are collectively called the standard library.
How can Python customize modules?
- Custom modules are in the same directory as the current code
The custom module hello.py is in the same directory as 1.py
The Hello module defines a module method
Use modules:
Advantages of this method: No additional configuration is required. In the same directory, you can directly import and use the same internal module. The disadvantage is obvious: the purpose of the existence of modules is to improve the reuse of code, so that multiple programs can share the code, if each module should be placed separately under the corresponding program, which is a little contrary to the starting point, so it is not recommended to use this method, understand it.
- Specify module locations in Python code
First in/home/dely/lot/python/library2 directory to define a module run. Py
Use the HI function in the run module in the following Demo
Advantages of this method: Common modules can be managed in the same way without being placed in the project directory, which improves reusability. Disadvantages: Before referencing, you need to specify the location of the module.
- The location of the Python module is specified by the environment variable
In the figure above, I have added PYTHONPATH to the environment variable. How to specify the environment variable is not covered here, but you can go to Google or Baidu according to your system version.
Once the environment variables are configured, write a fly module in the Library directory
Use the HI function of the FLY module in the following Demo
Advantages: code reuse, no need to formulate module path in the code, no redundant code, the same as using the standard library disadvantages: poor portability, different computers need to configure environment variables
The above three common methods, we can use according to their own needs.
Consider a question?
There is no problem managing a small project in this way, but what if we had 1000 modules with different functions? All in one folder?
The answer is definitely NO.
We need packages at this point to manage our modules.
For the sake of demonstration, I put it directly in the same directory as the project. Let’s have a look at the directory structure:
The EAT and say modules both have only one HI method.
The key points to note are indicated on the drawing.
Import Indicates the imported package. The package must be used. Module name. Method name used
from.. To import packages, you can directly use the module name. Method name use
summary
In this course, we will learn how to customize modules, customize packages, and import customized packages. There will be some pits in the process of use, so when you practice, check them out.
What’s next?
In the next lesson, we’ll look at common Python built-in modules. Only when we are familiar with the common modules and methods, we can use them flexibly and skillfully when doing projects.