python-day17 (formal learning)

package

First, what is the package?

Package is a form of a module, the essence is a packet containing the .pyfile folder.

Second, why have the 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, modular design by management module, maintenance will be more complicated, so we can use the package to extend the function of the module.

Third, how to use the package?

3.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

Introducing the package is introduced in the package .py, and may be introduced into the following two ways:

  1. import ...
  2. from ... import...

3.2 Expansion Module function

As if we need to expand aaa.py module, you 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 does 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.3 modify __init__.py 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())

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

Precautions

  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

Summed up the module does not come directly go and see https://www.cnblogs.com/nickchen121/p/10718112.html

Guess you like

Origin www.cnblogs.com/leaf-wind/p/11365810.html
Recommended