pyhton learning - package

  • Package: folder has a __init__.py file
  • Package: is a collection of several modules
    • # from import
    • # from glance.api import pickle
    • # from glance.api.policy import get
  • Directly into the package
    • # import glance
  • Introducing a package
    • This does not mean that the package contents are all below can be used
  • Importing a package in the end what happened?
    • Equivalent to the implementation of this package the following __init__.py file
  • Absolute imports
    • advantage:
      • Py in the implementation of a script, the script and the script and the same level of the module can only be written with absolute
    • Disadvantages:
      • 1. All import must next explain the relationship between a folder from the root directory
      • 2. If the current import package positional relationship between the imported files and packages sent on a change then all the __init__ file to do the appropriate adjustments
  • Relative imports:
  • advantage:
    • No need to repeatedly modify the path, as long as the relative positions of all the files in a package of folders and files are not changed does not need to be concerned about the current level of relations between the package and the file is executed
  • Shortcoming
    • py file contains relatively imported can not be directly executed, must be placed in the package to be introduced only if the call can execute # import modules from the package, then we do not need to do anything, directly into the line
  • import urllib
    • Error, indicating urllib a package can not be directly used resquest.py file
  • url.request
    • from urllib import request # request correct import method

Guess you like

Origin www.cnblogs.com/bilx/p/11366759.html