Modules are the basic way to organize your code in Python. Python scripts are saved with a text file with the extension py, a script that can be run separately, or it can be imported into another script. We say that the script that runs in the import other script is module.
1. How to import Scripts
The name of the module is the same as the script name, and if you define four operation operations functions, such as subtraction, in a file named operation.py:
Operation.py:#!/usr/bin/python#-*-coding:utf-8-*-def Jia (A, b): return a+b def Jian (A, B): Return a-b def Cheng (A, B): return a*b def Chu (A, b): Return A/ b
Modules can be introduced in several ways, using the functions defined in the module:
(1) Import operation the entire module
(2) from Operation Import * All elements in the module are imported
This way, each time you use the elements in the module, you don't have to add the module name every time.
(3) From Operation Import Jia, jian the element specified in the Import module
In this way, it is also not necessary to write the module name every time the element in the referenced module is used, and to distinguish it from the import all the way, which minimizes the possibility of introducing the elements in the module and the object name collisions in the introduction script.
(4) Import operation as OP renaming modules in the introduction script
In this way, the script code will be much more readable.
2, the introduction of the module specification
In general, all required modules are introduced at the beginning of the script, and the sequence recommendations for the various modules are broadly as follows:
- Python standard module
- Third-party modules
- Program Custom Module
Of course, the module can also be imported inside the function, so that the imported module scope is local.
3. Module Search Order
For the modules imported in the Python script, Python will find the imported module in the path specified by Sys.path, sequentially. Sys.path is generated dynamically when the script executes, mainly by the following three parts, Python will look for the imported modules in the following three paths:
- The location where the script executes, that is, the current path
- Pythonpath in environment variables (that is,. bash_profile)
- Dependency location when installing Python
The following shows the value of the Sys.path variable on my machine:
#!/usr/bin/python#-*-coding:utf-8-*-import sysfor p in Sys.path:
3. Examples of object persistence modules
The following is a module of memory object persistence that encapsulates the pickle module, making it easier for us to persist and restore objects in Python memory using picle.
Object_persistence.py:#!/usr/bin/python#-*-coding:utf-8-*-import Cpickle as Pickledef dump2file (obj, file_name): "' Dump the Obj_name object into a file named as file_name on disk. "F = open (file_name," WB ", True) pickle.dump (obj, f) f.close () return truedef restore_from_file (file_name): "Restore the object in the" the file named as file_name on disk, return as the returning value of the "this function ' ' F = open (file_name, "rb") obj = Pickle.load (f) f.close () return obj if __name__ = = "__main__": a = {"A": [ "B": [4,5,6]} dump2file (A, "Test_a.dat") print "The structure have been dumped to file." Print Restore_from_file ("Test_a.dat") print "Reading finished successfully." Module_try.py:#!/usr/bin/python#-*-coding:utf-8-*-from object_persistence Import Restore_from_file, Dump2fileif __ name__ = = "__main__": #read from the file a = {' A ': [1, 2, 3], ' B ': [4, 5, 6]} if Dump2file (A, "Test_a.dat"): Print "The object has been dump to file on disk. " Print "------Restoring and print------" Print restore_from_file ("Test_a.dat") print "Restore over!" $ Python module_try.py The object has been dump to file on disk.------Restoring and print------{' A ': [1, 2, 3], ' B ': [4, 5 , 6]}restore over!$All right, here we go. ^_^
Use of Python modules