We saw the functions and objects before. In essence, they are all for the better organization of existing procedures to facilitate reuse.
Modules (module) are also intended for the same purpose. In Python, a. py file forms a module. Through modules, you can invoke programs in other files.
Introducing Modules
We first write a first.py file, which reads as follows:
Copy Code code as follows:
Def laugh ():
print ' Hahahaha '
Write a second.py and introduce the program in the:
Copy Code code as follows:
Import a
For I in range (10):
First.laugh ()
In second.py, we used the laugh () function defined in first.py.
After the module is introduced, an object in the module can be invoked by means of a module. In the example above, the laugh () is the object introduced by the introduction of the module.
There are other ways to introduce Python
Copy Code code as follows:
Import a as B # to introduce module A and rename module A to B
From a import function1 # introduces the Function1 object from module A. When we call an object in a, we don't need to explain the module, which is directly using function1 instead of A.function1.
From a import * # introduce all objects from module A. When we call an object in a, we don't need to explain the module, which is to use the object directly, not the A. Object.
These reference methods can be easily written by the following programs.
Search Path
Python searches the following path for the module it wants to find:
The folder where the program resides
Installation path for the standard library
The path that the operating system environment variable Pythonpath contains
If you have a custom module, or a downloaded module, you can put it in the appropriate path according to the situation so that Python can find it.
Module Pack
Modules with similar functions can be placed in the same folder (for example, This_dir) to form a modular package. Pass
Copy Code code as follows:
Introduce module modules in the This_dir folder.
The folder must contain a __init__.py file that reminds Python that the folder is a module package. __init__.py can be an empty file.
Summarize
Import Module
Module.object
__init__.py