The Python module encapsulates one or more functional code sets for reuse. a module can be a file or a directory, and the Directory form is called a package. Module classification built-in modules can be understood as that after you have installed the python environment, you can directly use im... the Python module encapsulates one or more functional code sets for reuse. the module can be
File
It can also be
Directory
Directory is called
Package
.
Module category
Built-in modules
The built-in module can be understood to be used directly after you have installed the python environment.import
The imported module is the built-in module. the default module path is:C:\Python35\lib
You can also obtain the path of the built-in module in the following ways:
# Import sys module> import sys # The last Directory is the path of the built-in module> for n in sys. path :... print (n )... c: \ Python35 \ lib \ site-packages \ pip-8.1.1-py3.5.eggC: \ Python35 \ python35.zipC: \ Python35 \ DLLsC: \ Python35 \ libC: \ Python35C: \ Python35 \ lib \ site-packages
Third-party module
Third-party modules are usually written by developers and then submitted to the official python library, so that we can download and install them. the default installation directory isC:\Python35\lib\site-packages
,
Custom Module
Self-compiled modules
Module import method
Import a module as a whole
import sys
Import a specific variable or method from a module
from sys import path
Directly use the method name when callingpath
>>> path['', 'C:\\Python35\\lib\\site-packages\\pip-8.1.1-py3.5.egg', 'C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages']
Alias the imported module or method
from sys import path as path_alias
Alias used for callingpath_alias
>>> path_alias['', 'C:\\Python35\\lib\\site-packages\\pip-8.1.1-py3.5.egg', 'C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages']
Add the default environment variable, which is currently in effect
sys.path.append("PATH_NAME")
Availableimp
Modulereload
Method to re-load a module, for example, the following example:
$ cat simple.py #!/use/bin/env pythonprint('Hello, World!')spam = 1
>>> import simpleHello, World!>>> simple.spam1>>> simple.spam += 1>>> import simple>>> simple.spam2>>> import imp>>> imp.reload(simple)Hello, World!
>>> simple.spam1
Module import sequence
First, find the file with the same name as the import module in the current script Directory. If yes, import the file as a module (according to incomplete statistics, this is a pitfall, testre
The module is normal, but the testsys
The module is faulty)
Find out whether there is a corresponding module name under the module path.
If no module name is found, an error is returned.
How does import work?
The module performs the following three steps when being imported:
Find the module file through environment variables;
Compile it into a bytecode file. if there is a bytecode file, import the bytecode file;
Execute the code in the module to create the defined object;
The preceding three steps are performed only when the module is imported for the first time when the program is running. If this module has been imported and then imported again, the above three steps will be skipped, and it will directly extract the loaded module objects in the memory. The imported Python modules are saved insys.modules
Dictionary.
_ X and _ all __
All the variables in the module_
None of them startfrom *
Imported
$ cat simple.py #!/use/bin/env python_spam1 = 1spam2 = 1
>>> From simple import * >>> dir () # _ spam1 is not imported into ['_ builtins _', '_ doc __', '_ name _', '_ package _', 'spam2']
Opposite__all__
The variables in the list will befrom *
Imported, not in__all__
The variables in the list are not imported.
$ cat simple.py#!/use/bin/env python__all__ = ['spam2']spam1 = 1spam2 = 1
>>> From simple import * >>> dir () # spam1 is not imported into ['_ builtins _', '_ doc __', '_ name _', '_ package _', 'spam2']
Notes
According to incomplete statistics, if the name of the imported module contains this file in the current directory, the file under the current directory will only be used as the module, as shown below:
Create a script file named scripts
[root@iZ28i253je0Z ~]# touch scripts.py
Content is
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # import a module reimport re # output the matched string abcprint (re. match ('\ w +', "abc "). group ())
Execute scripts
[Root @ iZ28i253je0Z ~] # Python scripts. py # output the matching result abc.
Create.py
File namedre.py
[root@iZ28i253je0Z ~]# touch re.py
Content is
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # Output Content modusprint ("Hello Word ")
Execute againscripts.py
Script
[Root @ iZ28i253je0Z ~] # Python scripts. py # output Hello WordHello Word # and then report the error "no match" method Traceback (most recent call last): File "scripts. py", line 6, in
Print (re. match ('\ w +', "abc"). group () AttributeError: 'Module 'object has no attribute 'match'
Why? Becausepython
Setre.py
As a modulere
Now, let's continue:
Changescripts.py
The file content is as follows:
[root@iZ28i253je0Z ~]# cat scripts.py #!/usr/bin/env python# _*_ coding:utf-8 _*_import re
re.py
The file content remains unchanged, and then we are executing the scriptscripts.py
[root@iZ28i253je0Z ~]# python scripts.py Hello Word
See it. he willre.py
Get the code in the filescripts.py
File Execution. this is a pitfall. do not step on it.
Import files in subdirectories of the current directory
[Root @ ansheng ~] # Tree. /. /├ ── modules │ ├ ── _ init __. py │ ── lib01.py │ └ ── lib02.py ── scripts. py1 directory, 4 files [root @ ansheng ~] # Cat scripts. py #! /Usr/bin/env python # import the lib01 module under the modules package from modules import lib01 # import the lib02 module under the modules package from modules import lib02 [root @ ansheng ~] # Cat modules/_ init _. py #! /Usr/bin/env python [root @ ansheng ~] # Cat modules/lib01.py #! /Usr/bin/env python # lib01.py file will output "Hello lib01" print ("Hello lib01") [root @ ansheng ~] # Cat modules/lib02.py #! /Usr/bin/env python # The lib02.py file will output "Hello lib02" print ("Hello lib02 ")
Execution result
[Root @ ansheng ~] # Python scripts. py # The modules/lib02.py and modules/lib01.py file content Hello lib01Hello lib02 will be executed
You must declare__init__.py
File, even if the file is empty.
Original article link
The Python module encapsulates one or more functional code sets for reuse. the module can beFile
It can also beDirectory
Directory is calledPackage
.
Module category
Built-in modules
The built-in module can be understood to be used directly after you have installed the python environment.import
The imported module is the built-in module. the default module path is:C:\Python35\lib
You can also obtain the path of the built-in module in the following ways:
# Import sys module> import sys # The last Directory is the path of the built-in module> for n in sys. path :... print (n )... c: \ Python35 \ lib \ site-packages \ pip-8.1.1-py3.5.eggC: \ Python35 \ python35.zipC: \ Python35 \ DLLsC: \ Python35 \ libC: \ Python35C: \ Python35 \ lib \ site-packages
Third-party module
Third-party modules are usually written by developers and then submitted to the official python library, so that we can download and install them. the default installation directory isC:\Python35\lib\site-packages
,
Custom Module
Self-compiled modules
Module import method
Import a module as a whole
import sys
Import a specific variable or method from a module
from sys import path
Directly use the method name when callingpath
>>> path['', 'C:\\Python35\\lib\\site-packages\\pip-8.1.1-py3.5.egg', 'C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages']
Alias the imported module or method
from sys import path as path_alias
Alias used for callingpath_alias
>>> path_alias['', 'C:\\Python35\\lib\\site-packages\\pip-8.1.1-py3.5.egg', 'C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages']
Add the default environment variable, which is currently in effect
sys.path.append("PATH_NAME")
Availableimp
Modulereload
Method to re-load a module, for example, the following example:
$ cat simple.py #!/use/bin/env pythonprint('Hello, World!')spam = 1
>>> import simpleHello, World!>>> simple.spam1>>> simple.spam += 1>>> import simple>>> simple.spam2>>> import imp>>> imp.reload(simple)Hello, World!
>>> simple.spam1
Module import sequence
First, find the file with the same name as the import module in the current script Directory. If yes, import the file as a module (according to incomplete statistics, this is a pitfall, testre
The module is normal, but the testsys
The module is faulty)
Find out whether there is a corresponding module name under the module path.
If no module name is found, an error is returned.
How does import work?
The module performs the following three steps when being imported:
Find the module file through environment variables;
Compile it into a bytecode file. if there is a bytecode file, import the bytecode file;
Execute the code in the module to create the defined object;
The preceding three steps are performed only when the module is imported for the first time when the program is running. If this module has been imported and then imported again, the above three steps will be skipped, and it will directly extract the loaded module objects in the memory. The imported Python modules are saved insys.modules
Dictionary.
_ X and _ all __
All the variables in the module_
None of them startfrom *
Imported
$ cat simple.py #!/use/bin/env python_spam1 = 1spam2 = 1
>>> From simple import * >>> dir () # _ spam1 is not imported into ['_ builtins _', '_ doc __', '_ name _', '_ package _', 'spam2']
Opposite__all__
The variables in the list will befrom *
Imported, not in__all__
The variables in the list are not imported.
$ cat simple.py#!/use/bin/env python__all__ = ['spam2']spam1 = 1spam2 = 1
>>> From simple import * >>> dir () # spam1 is not imported into ['_ builtins _', '_ doc __', '_ name _', '_ package _', 'spam2']
Notes
According to incomplete statistics, if the name of the imported module contains this file in the current directory, the file under the current directory will only be used as the module, as shown below:
Create a script file named scripts
[root@iZ28i253je0Z ~]# touch scripts.py
Content is
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # import a module reimport re # output the matched string abcprint (re. match ('\ w +', "abc "). group ())
Execute scripts
[Root @ iZ28i253je0Z ~] # Python scripts. py # output the matching result abc.
Create.py
File namedre.py
[root@iZ28i253je0Z ~]# touch re.py
Content is
#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # Output Content modusprint ("Hello Word ")
Execute againscripts.py
Script
[Root @ iZ28i253je0Z ~] # Python scripts. py # output Hello WordHello Word # and then report the error "no match" method Traceback (most recent call last): File "scripts. py", line 6, in
Print (re. match ('\ w +', "abc"). group () AttributeError: 'Module 'object has no attribute 'match'
Why? Becausepython
Setre.py
As a modulere
Now, let's continue:
Changescripts.py
The file content is as follows:
[root@iZ28i253je0Z ~]# cat scripts.py #!/usr/bin/env python# _*_ coding:utf-8 _*_import re
re.py
The file content remains unchanged, and then we are executing the scriptscripts.py
[root@iZ28i253je0Z ~]# python scripts.py Hello Word
See it. he willre.py
Get the code in the filescripts.py
File Execution. this is a pitfall. do not step on it.
Import files in subdirectories of the current directory
[Root @ ansheng ~] # Tree. /. /├ ── modules │ ├ ── _ init __. py │ ── lib01.py │ └ ── lib02.py ── scripts. py1 directory, 4 files [root @ ansheng ~] # Cat scripts. py #! /Usr/bin/env python # import the lib01 module under the modules package from modules import lib01 # import the lib02 module under the modules package from modules import lib02 [root @ ansheng ~] # Cat modules/_ init _. py #! /Usr/bin/env python [root @ ansheng ~] # Cat modules/lib01.py #! /Usr/bin/env python # lib01.py file will output "Hello lib01" print ("Hello lib01") [root @ ansheng ~] # Cat modules/lib02.py #! /Usr/bin/env python # The lib02.py file will output "Hello lib02" print ("Hello lib02 ")
Execution result
[Root @ ansheng ~] # Python scripts. py # The modules/lib02.py and modules/lib01.py file content Hello lib01Hello lib02 will be executed
You must declare__init__.py
File, even if the file is empty.
For more articles about The Python Standard Library series, refer to the PHP Chinese network!