009: Modules and Pachage

这次学习只做初步的了解,不深入学习,以后当开始做项目的时候,再回过头来学习。

所谓的模块其实就是一个.py的文件,一个.py的文件就是一个模块。

比模块更大的是包,其实所谓的包其实就是一个文件夹,但是这个文件夹内必须有一个__init__.py的文件, 这个文件可以定义一些这个包的配置

导入的时候,可以直接用以下方式来导入:

import module_name
import module_name as X

import package_name.module_name 
import package_name.module_name as Y

from package_name import module_name
from package_name.module_name import obj_name

from module_name import obj_name

# 同一文件夹时可以这样导入
from .module_name import obj_name

  

1. Modules

  1). A module is a file containing Python definiations and statements. Definitions from a module can be imported into other modules or into the main module (the collection of variables that you have access to in a script executed at        the top level and in calculator mode). The file name is the module name with the suffix .py appended. Within a module, the module's name is available as the value of the global variable __name__.

  2). When we import a module,  the definitions and statements are executed only the first time the module name is encountered in an import statement.

  3). 如果我们用 import module_a ,那么我们如果需要访问module_a 里面的数据时,可以使用 module_a.content(可以是一个变量,一个方法,也可以是一个class) 来访问.

       如果我们用 from module_a import * ,则可以直接调用模块里面的任意内容,这种方法一般比较少,可读性不高

       如果我们用 from module_a import content, 则可以直接用content, 这种写法最常见。

  4). 直接运行脚本时也支持命令行参数,调用方法为: python xx.py <arguments>

     xx.py 中引用变量的方法是 sys.argv[1]

2. Packages

  1) packages are a way of structuring Python’s module namespace by using “dotted module names”. For example, the module name A.B designates a submodule named B in a package named A.

         

    2) The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such asstring, from unintentionally hiding valid modules that occur later on        the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, import * would import the content for the variable of                 __all__.

     __all__ = ["echo", "surround", "reverse"]

     3)When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to               use the echo module in the sound.effects package, it can use from sound.effectsimport echo.

         You can also write relative imports, with the from module import name form of import statement. These imports use leading dots to indicate the current and parent packages involved in the relative import. From                                the surround module for example, you might use:

from . import echo
from .. import formats
from ..filters import equalizer

        Note that relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application must always use absolute             imports.

    4) 关于path目前只做了解,以后再回头来学习。

        By definition, if a module has an __path__ attribute, it is a package, regardless of its value.

        A package’s __path__ attribute is used during imports of its subpackages. Within the import machinery, it functions much the same as sys.path, i.e. providing a list of locations to search for modules during import.                                 However, __path__ is typically much more constrained than sys.path.

    __path__ must be an iterable of strings, but it may be empty. The same rules used for sys.path also apply to a package’s __path__, and sys.path_hooks(described below) are consulted when traversing a package’s __path__.

原文地址:https://www.cnblogs.com/jcsz/p/5110504.html