Chapter IX, package

Chapter IX, package

First, what is the package

One form is the module package, the nature of the package is the file containing the .py file folders, as well as __init__ file, you must have this file, otherwise, it is an ordinary folder

Second, why have package

The first version of the module is only 10 function, but in the future when the extended version, module name and usage should be best not to change, but this is only for user-friendly, and because the extended version, files increases, the module designers management module, maintenance will be more complicated, so we can use the package to extend the function of the module.

Third, how to use package

  1. Modules and packages

    Import module three things happen:

    1. Create a namespace package

    2. Py file execution, the execution name generated in the process is stored in the namespace.

    3. Aaa get a name in the current executable file, aaa is pointing package namespace

    Importing package three things happen:

    1. Create a namespace package
    2. Since the package is a folder, you can not execute the package, so .py files in the package is executed, the execution name generated in the process is stored in the package namespace (namespace name that is stored in the package are from .py)
    3. Aaa get a name in the current executable file, aaa is pointing package namespace
  2. Expansion Module function

    As if we need to expand aaa.py module, need to create a catalog file aaa, and delete aaa.py file, aaa.py modified to m1.py and m2.py two files, make use of the function module is not change.

    # aaa.py
    
    def func1():
        pass
    
    def func2():
        pass
    
    def func3():
        pass
    
    def func4():
        pass
    
    def func5():
        pass
    
    def func6():
        pass
    # m1.py
    
    def func1():
        pass
    
    def func2():
        pass
    
    def func3():
        pass
    # m2.py
    
    def func4():
        pass
    
    def func5():
        pass
    
    def func6():
        pass
    # run.py
    
    import aaa
    
    aaa.func1()
    aaa.func2()
    aaa.func3()
    aaa.func4()
    aaa.func5()
    aaa.func6()
  3. Modify __init__ file

    # aaa.py
    
    func1 = 111
    func2 = 222
    func3 = 333
    func4 = 444
    func5 = 555
    func6 = 666

    Since __init__.pydefined func1, so we can import func1 in run.py file, but this is not what we want func1 func1, it is necessary to modify __init__.pythe file, and because the executable file run.py environment variable is not aaa, therefore directly import import m1 will complain, so use from import.

    # aaa/.py
    
    from aaa.m1 import func1
    from aaa.m2 import func2
    # run.py
    
    import aaa
    
    
    print(aaa.func1())
    print(aaa.func2())
  4. Encapsulated packet introduced

  5. aaa.bbb pointing inside the folder aaa bbb package, if we need to import bbb this package.

    # bbb/.py
    
    from aaa import bbb
    # run.py
    
    import aaa
    
    
    print(aaa.bbb)
  6. Introduced into the module encapsulated packet

    # bbb/.py
    
    from aaa.bbb import m3
    # run.py
    
    import aaa
    
    
    aaa.bbb.m3
  7. Importing absolute and relative import

    Absolute imports

    # aaa/.py
    
    from aaa.m1 import func1
    from aaa.m2 import func2

    Relative imports

    • The representative of the current file import folder is located
    • .. on the first level represents the current file is located in the import folder
    • ... on behalf of the current level on a file where the import folder
    from .m1 import func1
    from .m2 import func2

IV Notes

  1. All files in the package are imported using, instead of being run directly

  2. Introduced between the inner bag module can use absolute introduced (in the root directory as a reference packet) relative introduced (in the current directory where the module is introduced as a reference), recommended relative import

  3. When the file is executable file, not a relative import syntax, and only when the file is imported as a module within the file within the file in order to use the syntax of relative imports

  4. Those who a little while importing the left point must be a package import aaa.bbb.m3.f3error

    Figure

  5. 65包-目录结构.jpg?x-oss-process=style/watermark

    You can only import aaa.bbb.m3

Guess you like

Origin www.cnblogs.com/demiao/p/11366539.html