Day24-- python learning modules and packages + project development specification

[Knowledge]

1, the module

  (1)from...import.... 

    # Introduced what will be able to use, does not import variables can not be used;

    # Do not import does not mean that does not exist, but does not create a file name to reference other modules;

    # When the method or variable import module and this document when the same name, then the name is that it represents the last method or variable assignments;

1  from my_module Import the Login
 2  # introduced what will be able to use, you can not use variable is not imported 
3  # Do not import does not mean that does not exist, but does not create a file name to reference other modules in 
4  DEF the Login ():
 5      Print ( " in the Login My " )
 6  # that method or variable when a method or variable modules imported and this document when the same name, then the name assigned to it represents the last 
7 the Login ()

  (2) from ... import ... as ... (name, imported s)

1 from my_module import login as l,name as n

  (3)from...import *

    # Import modules can use everything

    # __All__ can import content control *

 1 __all__ = ['login']
 2 
 3 name = 'alex'
 4 
 5 def login():
 6     print('login',name)
 7 
 8 import sys
 9 my_module = sys.modules[__name__]
10 getattr(my_module,'login')()

[Note] At this time, other references to the module, the control can only export * login, the other can not be imported by *

  (4) other knowledge-related modules

    # The module as a script: in this module the variables reflected from this module

    # Module search path

    # Reload module

    # Pyc compiler file

  ①  the module as a script:

    # Run a py file in two ways:

      A, operation of the module in the form of import my_module

        If you do not want to perform in the case of import module (such as module in a print () etc.): Add the following in the module :( scripts are run __name __ = '__ main__')

1  if __name__ == '__main__':
2      print('饿了么')
3      print(__name__,type(__name__)) # '__main__'/'my_module'

【note】:

    # Py in the preparation of documents, not all functions and content classes in the package should be written in __name __ == '__ main__' if: under (shortcuts, print main press tab)

    # When using a transmit their own content module, you should write the following:

. 1  Import SYS 
 2 getattr (the sys.modules [ the __name__ ], ' variable name ' )

      B, directly run cmd run pycharm

  ② pyc compiled files and reload module

    Reload :( can not be used when developing, holding a play)

    # Then modified after import this module is imported, the program is imperceptible, reload this way can force the program to re-import again

. 1  Import AAA
 2  Import the importlib
 . 3  
. 4 importlib.reload (AAA)   # represents reload

   (4) recycling module REFERENCE

    In the import module, do not produce a circular reference problem

2, including: a set of collection py document provides a set of complex functions.

  # Why should pack it? When the functionality provided more complicated, less than a py file write time

    What # bag with? At least one package __init__.py

  (1) Simple introduced:..... Import packet module packet - variable packet module package (use)

            from the package. import module package (recommend this)

  (2) introduced into the package equivalent to a package performed following __init__.py file (source when reading frame)

    # Absolute import module package:

      ① write a next packet to be introduced in the first stage packet following __init__.py file: from import package bag

      ② To import written packet in the second stage under the following file __init__.py a module or a package:. From import package bag package / module

      So be down

    # Bag module relative introduced:

      ① from. Import module (which. Is equivalent to the current directory, .. indicates the parent directory) are written in the following package file __init__.py

      ② above absolute import of the same idea, but with Get relative path

      ③ can be introduced relatively module executed, a script can not be performed in

  (3) from a full bag module: acquiring path module, and then add the path: sys.path.append (path)

  (4)从一个包引用另一个包里的模块:

 1 import sys
 2 # 获取路径
 3 ret=__file__.split('/')  # ['D:', 'wendang', 'PyCharmCode', 'MyPycharm', 'day24 模块', 'fff', 'bbb', 'main.py']
 4 base_path='/'.join(ret[:-2]) # 插入‘/’
 5 # 添加路径
 6 sys.path.append(base_path)
 7 print(base_path)
 8 # 导入模块
 9 from xxx import hello
10 hello.func()

3、项目开发规范


时间:2020-02-18          01:07:35

Guess you like

Origin www.cnblogs.com/fengxb1213/p/12321430.html