Python modules reference mechanism

First, the reference module

  • Def: Use class (method), a function defined in another file data program like Python

  • Referenced module position. usually
    • Python2 : "/Library/Python/2.7/site-packages/"
    • Python3: “/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/”
    • Can be viewed using sys module modules. code show as below
import sys
sys.modules['os']    # 查看 模块“os”的路径

Second, the reference mechanism

  • Method 1: import XXX ; XXX.py is a python file

    • XXX.py introduced directly into the files of all content definitions.
  • Second way: from XXX import yyy ; XXX.py is a python file

    • Yyy defined XXX.py introduced directly into the file. yyy is the class (method), a function, data, etc.
  • Three import XXXways: ; XXX is a directory

    • All content is introduced directly into the definition XXX directory.
  • Four from XXX import yyyways: ; XXX is a directory

    • Introduced directly into the definition of XXX yyy directory. yyy is the class (method), a function, data, etc.
  • Five ways: from XXX.zzz import yyy ; XXX is a directory , and zzz is its subdirectories

    • Direct introduction yyy zzz subdirectory under the definition of XXX directory. yyy is the class (method), a function, data, etc.

Guess you like

Origin www.cnblogs.com/juking/p/11220175.html