Introduction to module and package concepts in Python

  • 2020-05-09 18:45:48
  • OfStack

The module overview

If modules are the way to organize Python code logically, then files are the way to organize modules at the physical level.
Therefore, **1 file is regarded as a separate module, and 1 module can also be regarded as a file. The file name of a module is the module
The name of the block with the extension.py. Unlike other languages that can import classes (class), in Python you import modules or module properties **.

Module namespace

A namespace is a relational mapping of a name to an object.

The import module

Import module whole (import)

Method 1


import module1
import module2
...
import moduleN

Way 2

import module1[, module2[,... moduleN]]

Import module properties (from... import...).

Calling from-import imports the name into the current namespace, which means you don't need to use the property/period property identifier to access the module's identifier. For example, you need to access the var name in the module module to be imported like this:


from module import name1[, name2[, ... nameN]]

Rename the module or property after import

import ... as ...
from ... import ... as ...

package

A package is a hierarchical file directory structure, with modules corresponding to a single file and packages corresponding to a directory. Causes the module in the package to be imported with standard import and from-import statements.


Related articles: