pyhton package

 

What is the package

Through the use of package is a 'module name' python modules organized manner namespace.

In layman's terms, a package that contains the init .py files in a folder, so in fact our purpose is to create a package with the file folder / modules organized.

It is emphasized that:

  1. In python3, the package is not even in the init .py file, import the package is still not being given, but in python2, the next package must have the file, or import the package error

    1. The purpose is not to create a package to run, but was introduced into use, remember, is only one form of bag module only, the package is a modular nature.

      1. Reminder: When running the executable file, sys.path will automatically execute files in the current directory in sys.path added, so if this module or package in the current path of the executable file that you can import into.

Three things happened references a package of
  • To create a package named namespace in memory

  • Automatically execute __init__.pyfile

  • Introduced by the package name. The way __init__.pyfile names of all

Why use package

The nature of the package is a folder, then the only function is to tell the file folder to organize with the increase in functionality, we can not all of the functions placed under a folder, so we use the module to organize functions, but with the module more and more, we need to organize your files into folders, thereby improving the structural and maintainability of the program.

Use package

1. Examples of file

Note: The executable file with the same level package

  

2. File Content

__init__ .py
 # 1 
# from BaO Import FF 
# 2 
# from bao.ff Import file_, PRIN 
ff.py 
file_ = ' Hello! ' DEF PRIN ():
     Print ( " ? Output ah " ) 
executable file .py # Import BaO 
# Print (bao.ff.file_) # Print (bao.file_) 
# bao.prin ()


  

 

Use of the package from ... import ...

Note that after the import module import from, must be clear not a little, or there will be a syntax error, such as: from a import bc wrong syntax

1  from bao.ff Import file_, PRIN # correct 
2  from bao.ff Import *   # import all
Importing absolute and relative import

Our top package glance was written by someone else, and then there will be demand for imported between each other in mutual glance inside the package, this time there is an absolute and relative import import in two ways:

Absolute import: to bao as a starting

Introducing relative: .. manner or with most start (only in a package, it can not be used within different directories).

File Structure:

  

Code examples

Executable file .py 
Import NB nb AS
 Print (nb.mn1)
 Print (nb.mn2)
 Print (nb.mn3) 
nb.func1 () 
nb.func2 () 
nb.func3 () 
__init__ .py 
# absolute, introducing relative 
# from NB.m1 import MN1 
# from NB.m2 import Mn2 
# from NB.m3 import MN3 
# opposing introduced from .M1 import MN1, func1
 from .m2 import Mn2, func2
 from .m3 import MN3, func3 
three a package file contents 
mn1
 = " I'm M1 " 
DEF func1 ():
     Print ( ' M1 ' ) 
Mn2 = ' I am M2 ' 
DEF func2 ():
     Print ( ' M2 ' ) 
mn3 = ' I M3 ' 
DEF func3 ():
     Print ( ' M3 ' )
Absolute absolute import import summary

Absolute imports : to sys.path executable file as a starting point to begin importing, called absolute imports

Advantages : the module is introduced into the execution file may be used in

Cons: All are imported to sys.path as a starting point, the import of trouble

Relative import : reference to a current location of the folder starts searching, called relative introduced

Symbol : document represents the current location of the file plus, .. on behalf of a folder on a file level ... on behalf clip

Pros: Import easier

Drawback : it can only be used when importing package module

     #note:         

        1. introduced only for mutual relative introduced between the inner bag module, is introduced by the importer must exist in an inner bag

          2. trying to use relative imports are wrong, meaning, you must use relative imports of a premium package in addition to the top-level package, each additional representative to jump on a folder, on a package should not exceed the top

Guess you like

Origin www.cnblogs.com/z-x-h/p/12049199.html