Modular python

A modular

  •  Generally, programming languages, database, package, module is the same concept, the code organization
  •  python only one module object, but in order to facilitate the modular organization module, it provides a concept - package, the module is the same concept, is the code organization
  •  Module module, referring to source files python
  •  Package package, the value is the directory and package the same name as the module organization and its related files together

Second, the import statement

  •  import module 1, module 2: full import module
  •  import ... as ...: Alias ​​module

1, import statements

  • Find the specified module, load and initialize it to generate module object can not be found, an exception is thrown ImportError; namespace in the local scope of import is located, and the associated increase in the name of the object created in the previous step
Import functools   # import the top-level module 
Import os.path     # import non-top-level module 
Print (dir ())
 Print (functools)     
    to import top-level module, its name will be added to the local namespace, and bind to its target module 
    to import non-top-level module , its top-level module added to the local namespace, import module must use the fully qualified name to access 
    if used as, subsequent names added directly into the local namespace and bind directly to the imported module object
    


2, from ... import introducing portion

  •    from ... import ... as ... Synonyms
  •    from pathlib import Path, PosixPath # import the module specified member in the current namespace
  •    from pathlib import * # import all public members of the module in the current namespace
  •    from functools import wraps as wr  #别名
Example. 1:  
     from os.path Import EXISTS   # loading, initialization module os.path, exists local name space is added and binds 

    IF EXISTS ( ' C: \'): 
              Print ( ' Found ' ) 
              
    the else :
               Print ( ' Not Found ' ) 
        

    Print (the dir ())
     Print (EXISTS) 

    to find the module specified in the from clause, it loads and initializes 
    the corresponding name after import clause
         1 , clause introduction form first check whether the module name attribute
         2 , if not, then try to import the name of the sub-module
         3 , have not found, an exception is thrown ImportError
         4, save the name to the local name space, as if clause, after the name is used as clause


Third, the custom module naming

  •  1, the module name is the file name
  •  2, the module name must conform to the requirements of a combination of identifiers, non-alphanumeric number at the beginning and underlined
  •  3, do not use the system module name, to avoid conflict, unless you know the purpose of this module name 
  •  4, usually module name all lowercase, underscores to separate

 

Fourth, the module search order

1, using sys.path View search order

import sys

    for p in sys.path:
        print(p)
    
执行输出:
    c:\users\administrator\appdata\local\programs\python\python35\python35.zip
    c:\users\administrator\appdata\local\programs\python\python35\DLLs
    c:\users\administrator\appdata\local\programs\python\python35\lib
    c:\users\administrator\appdata\local\programs\python\python35
    c:\users\administrator\appdata\local\programs\python\python35\lib\site-packages
    c:\users\administrator\appdata\local\programs\python\python35\lib\site-packages\IPython\extensions
    C:\Users\Administrator\.ipython
    
     Showing results, the path search order python module when a module is loaded when needed from the search path from front to back in order to find 
    does not search subdirectories of these directories, search on the load, search not to throw an exception


2, the path order

  •   The main program directory, run the main script directory where
  •   PYTHONPATH directory, the directory also set the environment variable PYTHONPATH search path module
  •   Standard library catalog, python built-in library modules directory where

3. Repeat the import module

  • All modules are loaded in the recording sys.modules, sys.modlues stored dictionary with all loaded modules, the repeated introduction absent

4, module run

  • __name__, each module will define a __name__ special variable to store the name of the current module, if not specified, it defaults to the source code files noun, if the package is limited naming interpreter initialization time, initializes sys.modules dictionary (save the module loaded), create builtins module,
  • __main__ module, sys module, and the module search path sys.path

5、if __name__ == '__main__':用途

  • Function within the functional test of the module, this module test
  • Avoid the side effects of changing the main module, the package code is not the top layer, the main module with no problems, but once a new master module, this module is to be introduced, because the original code is not packaged, is performed together with

Fifth, the module properties

  •  __file__: string, the source file path
  •  __cached__: String, byte code compiled file path
  •  __spec__: a display specification module
  •  __name__: module name
  •  __package__: When the module is a package, with __name__, otherwise, you may be set to an empty string of top-level module

1, a special package bag module

Modules and packages summary:

  • Better organization module package, especially a large number of code modules, can be split into many sub-module, it is easy to use certain functions to load the corresponding sub-module;
  • __Init__.py package directory will be executed when the package is first introduced, content can be empty, can also be used for initialization code for the package, it is best not to delete it;
  • Import submodule parent module will load, but will not be introduced into the parent module import submodule can only be used as a spacer dot character between the package directory, it represents a hierarchical relationship module and submodule
  • Modules are packaged, such as the same function. But he was able to package variables, classes, function
  • Module is the name of space inside the top identifiers, are his property, can () or dir viewed by __dict__

 

Sixth, the absolute and relative import import

1, absolute imports

  • Import module, the module name does not begin with the foremost point is absolutely in the import statement to import or from
  • Import absolutely always go to the search module search path to find

2, the relative introduced

  • It can only be used within the package, and can only be used from the statement, using the Point number is provided, within the current directory
  •  .. a directory represented by the above, do not use the top-level module relative imports

 

Guess you like

Origin www.cnblogs.com/jiangzuofenghua/p/11450579.html