Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
We have seen functions and objects before. Essentially, they are all for better organizations.ProgramTo facilitate reuse.
Module)For the same purpose. In python,A. py file forms a module.. Through the module, you canCall programs in other files.
1. import and use modules
First, write a first. py file with the following content:
DefLaugh ():Print 'Hahahaha'
Write another second. py
ImportFirstForIInRange (10): First. Laugh ()
In second. py, we didn't define the laugh function, but by introducing (import) from first, we can directly use the laugh function in first. py.
We can see from the above that after the module is introduced, we canModule. ObjectTo call the desired object. In the above example, the first is the introduced module, and laugh () is the object we introduce.
In addition, there are other import methods, such as import a as B, from a import *, which are all reasons for convenient writing, essentially no difference.
2. Search Path
Python will search for the module it is looking for in the following path:
1. folder of the program
2. installation path of the standard library
3. Path included in the operating system environment variable pythonpath
If you have a custom module or a downloaded module, you can put it in the corresponding path as needed so that python can find it.
3. Module package
You can place modules with similar functions in the same folder (such as DIR ).
ImportDir. Module
.
Note: This folder must contain_ Init _. pyTo remind Python that the folder is a module package. _ Init _. py can be an empty file.
Summary
Import Module
Module. Object
_ Init _. py