Python的Module,Library,Package的区别

目录

1、背景

2、module的简介

3、library的简介

4、package的简介

5、Python中的module和library之间的区别

6、Python中的module和package之间的区别

7、包的使用

8、包的一些问题

1、背景

 Python中的module,library,package之间的区别。

2、module的简介

 module:模块

 Python中的module,说白了就是Python文件,而Python文件一般后缀为py,所以就是你的xxx.py

3、library的简介

 library:库,也常称为:库文件。

 之所以此处不说时Python的library,那是因为,本省library这个词,一般都是针对其他的编译型语言,比如c,c#等语言来说的。常见的c/c#等语言中的library,一般指的就是:

  • 静态的库文件:xxx.a
  • 动态的库文件:xxx.dll

4、package的简介

 package:包

 Python中的package,可以简单的理解为,一组的module,一堆(相关)module组合而成的。通常包总是一个目录,可以使用import导入包,或者from+import来导入包中的部分模块。包目录下方为首的一个文件便是init.py。然后是一些模块文件和子目录,假如子目录中也有int.py,那么它就是这个包的子包了。

5、Python中的module和library之间的区别

 对于library和module,说白了,都是提供了一定的功能供别人调用。从这方面来说,也可以理解为:Python中的library等价于module;只不过Python中很少说library,正常情况下都说module。

 所以,简而言之:

  • library多数都是指的是C,C#等语言中的库,库文件;Python中很少用library这个词。
  • Python中的“库”称为“module”——模块。

6、Python中的module和package之间的区别

 导入单个的module,一般是这样的:

import my_module

 导入package一般是这样的:

from my_package.timing.dager.internets import function_of_love

 可以理解为:

  • module:单个的模块,一般是单个(偶尔为多个)python文件;
  • package:多个相关的module的组合。肯定是多个,相关的Python文件的组合;Package是用来把相关的模块组织在一起,成为一个整体的。

7、包的使用

 多个关系密切的模块应该组织成一个包,以便于维护和使用。这项技术能有效避免名字空间冲突。创建一个名字为包名字的文件夹并在该文件夹下创建一个init.py文件就定义了一个包。你可以根据需要在该文件夹下存放资源文件、已编译扩展及子包。

 举例来说,一个包可能有以下结构:

Graphics/
      __init__.py
      Primitive/
         __init__.py
         lines.py
         fill.py
         text.py
         ...
      Graph2d/
         __init__.py
         plot2d.py
         ...
      Graph3d/
         __init__.py
         plot3d.py
         ...
      Formats/
         __init__.py
         gif.py
         png.py
         tiff.py
         jpeg.py

 import语句使用以下几种方式导入包中的模块:

import Graphics.Primitive.fill #导入模块Graphics.Primitive.fill,只能以全名访问模块属性,例如 Graphics.Primitive.fill.floodfill(img,x,y,color).  
from Graphics.Primitive import fill# 导入模块fill ,只能以 fill.属性名这种方式访问模块属性,例如 fill.floodfill(img,x,y,color).  
from Graphics.Primitive.fill import floodfill #导入模块fill ,并将函数floodfill放入当前名称空间,直接访问被导入的属性,例如 floodfill(img,x,y,color). 

 无论一个包的哪个部分被导入,在文件init.py中的代码都会运行,这个文件的内容允许为空,不过通常情况下它用来存放包的初始化代码。导入过程遇到的所有init.py文件都被运行。因此,import Graphics,Primitive.fill语句会顺序运行Graphics和Primitive文件夹下的init.py文件。

8、包的一些问题

 下面这个语句具有歧义:

from Graphics.Primitive import * 

 这个语句的意图是将Graphics.Primitive包下的所有模块导入当前的名称空间。然而,由于不同平台间文件名规则不同(比如大小写敏感问题),Python不能正确判定哪个模块要被导入。这个语句只会顺序运行Graphics和Primitive文件夹下的init.py文件,要解决这个问题,因该在Primitive文件夹下面的init.py中定义一个名字all的列表,例如:

# Graphics/Primitive/__init__.py  
__all__ = ["lines","text","fill",...]  

 这样,上边的语句就可以导入列表中所有模块。

 下面这个语句只会执行Graphics目录下的init.py文件,而不会导入任何模块:


import Graphics  
Graphics.Primitive.fill.floodfill(img,x,y,color)  # 失败!  

 不过既然import Graphics语句会运行Graphics目录下的init.py文件,我们就可以采取下面的手段来解决这个问题:

# Graphics/__init__.py  
import Primitive, Graph2d, Graph3d  
# Graphics/Primitive/__init__.py  
import lines, fill, text, ... 

 这样import Graphics语句就可以导入所有的子模块(只能用全名来访问这些模块的属性)

Reference

Python3.6标准库 It contains data types that would normally be considered part of the “core” of a language, such as numbers and lists. For these types, the Python language core defines the form of literals and places some constraints on their semantics, but does not fully define the semantics. The library also contains built-in functions and exceptions — objects that can be used by all Python code without the need of an import statement. Some of these are defined by the core language, but many are not essential for the core semantics and are only described here. The bulk of the library, however, consists of a collection of modules. There are many ways to dissect this collection. Some modules are written in C and built in to the Python interpreter; others are written in Python and imported in source form. Some modules provide interfaces that are highly specific to Python, like printing a stack trace; some provide interfaces that are specific to particular operating systems, such as access to specific hardware; others provide interfaces that are specific to a particular application domain, like the World Wide Web. Some modules are available in all versions and ports of Python; others are only available when the underlying system supports or requires them; yet others are available only when a particular configuration option was chosen at the time when Python was compiled and installed. This manual is organized “from the inside out:” it first describes the built-in functions, data types and exceptions, and finally the modules, grouped in chapters of related modules. This means that if you start reading this manual from the start, and skip to the next chapter when you get bored, you will get a reasonable overview of the available modules and application areas that are supported by the Python library. Of course, you don’t have to read it like a novel — you can also browse the table of contents (in front of the manual), or look for a specific function, module or term in the index (in the back). And finally, if you enjoy learning about random subjects, you choose a random page number (see module random) and read a section or two. Regardless of the order in which you read the sections of this manual, it helps to start with chapter Built-in Functions, as the remainder of the manual assumes familiarity with this material.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值