Python学习笔记(二十三)模块包(Module Packages)

1.在模块包(module package)目录中__init__.py文件的用途是什么?
答:__init__.py文件用于声明和初始化常规模块包; Python首次导入进程中的目录时会自动运行其代码。 其分配的变量成为在内存中创建的模块对象的属性,以对应于该目录。 它在3.3及更高版本之前也不是可选的 - 除非包含此文件,否则无法通过包语法(package syntax)导入目录。

2.每次引用包的内容时,如何避免重复完整的包路径?
答:将from语句与包一起使用可直接从包中复制名称,或使用带有import语句的as扩展名将路径重命名为较短的同义词。 在这两种情况下,路径仅在from或import语句中的一个位置中被列出。

3.哪些目录需要__init__.py文件?
答:在Python 3.2及更早版本中,执行的import或from语句中列出的每个目录都必须包含__init__.py文件。 其他目录(包括包含路径最左边上的组件的目录)不需要包含此文件。

4.何时必须使用import而不是from来处理包?
答:仅当您需要访问在多个路径中定义的相同名称时,才必须使用import而不是from来处理包。 使用import时,路径会让引用唯一,但from只允许任何给定名称的一个版本(会覆盖相同的名字)(除非您还使用as扩展名重命名)。

5. from mypkg import spam 与 from . import spam 有什么区别? 
答:在Python 3.X中, from mypkg import spam 是绝对导入 - 搜索mypkg会跳过软件包目录,模块位于sys.path中的绝对目录中。语句 from . import spam, 另一方面,是一种相对导入 - 会查找spam相对于仅包含此语句的包。 在Python 2.X中,绝对导入在执行sys.path之前首先搜索包目录; 相对导入如上所述。

6.什么是命名空间包(namespace package)?
答:命名空间包是导入模型的扩展,可在Python 3.3及更高版本中使用,它对应于一个或多个没有__init__.py文件的目录。 当Python在导入搜索期间找到它们,并且起初没有找到简单模块或常规包时,它会创建一个命名空间包,它是所有找到的具有所请求模块名称的目录的虚级拼接(virtual concatenation)。 会在所有命名空间包的目录中查找更多嵌套成分。 效果类似于常规包,但内容可能分为多个目录。

(以上翻译可参看文末的原文,这部分我有点儿懵(。・_・。)ノI’m sorry~)

一些其它的东西:
1.Package import 

(1)import dir1.dir2.mod

(2)from dir1.dir2.mod import X

2.避免在代码中出现 import C:\mycode\dir1\dir2\mod #错误语法

但是你可以把 C:\mycode添加到PYTHONPATH变量或者.pth文件中

使用 import dir1.dir2.mod

3.点搜索语法

之所以 import mod.py 在Python中错误的原因是:它会被Python解释成加载 mod\py.py

注:转载《Learning Python 5th Edition》[奥莱理]

1. What is the purpose of an __init__.py file in a module package directory?
2. How can you avoid repeating the full package path every time you reference a package's content?
3. Which directories require __init__.py files?
4. When must you use import instead of from with packages?
5. What is the difference between from mypkg import spam and from . import spam?
6. What is a namespace package?

1. The __init__.py file serves to declare and initialize a regular module package; Python automatically runs its code the first time you import through a directory in a process. Its assigned variables become the attributes of the module object created in memory to correspond to that directory. It is also not optional until 3.3 and later—you can’t import through a directory with package syntax unless it contains this file.
2. Use the from statement with a package to copy names out of the package directly, or use the as extension with the import statement to rename the path to a shorter synonym. In both cases, the path is listed in only one place, in the from or import statement.
3. In Python 3.2 and earlier, each directory listed in an executed import or from statement must contain an __init__.py file. Other directories, including the directory that contains the leftmost component of a package path, do not need to include this file.
4. You must use import instead of from with packages only if you need to access the same name defined in more than one path. With import, the path makes the references unique, but from allows only one version of any given name (unless you also use the as extension to rename).
5. In Python 3.X, from mypkg import spam is an absolute import—the search for mypkg skips the package directory and the module is located in an absolute directory in sys.path. A statement from . import spam, on the other hand, is a relative import —spam is looked up relative to the package in which this statement is contained only. In Python 2.X, the absolute import searches the package directory first before proceeding to sys.path; relative imports work as described.
6. A namespace package is an extension to the import model, available in Python 3.3 and later, that corresponds to one or more directories that do not have __init__.py files. When Python finds these during an import search, and does not find a simple module or regular package first, it creates a namespace package that is the virtual concatenation of all found directories having the requested module name. Further nested components are looked up in all the namespace package’s directories. The effect is similar to a regular package, but content may be split across multiple directories.

猜你喜欢

转载自blog.csdn.net/Enderman_xiaohei/article/details/88089415