Python 中的模块和库有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19198166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whats the difference between a module and a library in Python?
提问by joker
I have background in Java and I am new to Python. I want to make sure I understand correctly Python terminology before I go ahead.
我有 Java 背景,而且我是 Python 新手。在继续之前,我想确保我正确理解了 Python 术语。
My understanding of a moduleis: a script which can be imported by many scripts, to make reading easier. Just like in java you have a class, and that class can be imported by many other classes.
我对一个模块的理解是:一个脚本,可以被多个脚本导入,方便阅读。就像在 Java 中一样,您有一个类,并且该类可以由许多其他类导入。
My understanding of a libraryis: A library contains many moduleswhich are separated by its use.
我对库的理解是:库包含许多模块,这些模块按用途分开。
My question is: Are libraries like packages, where you have a package e.g. called food
, then:
我的问题是:库是否像包一样,在那里你有一个包,例如叫做food
,然后:
- chocolate.py
- sweets.py
- biscuts.py
- 巧克力.py
- 糖果.py
- 饼干.py
are contained in the food
package?
包含在food
包中?
Or do libraries use packages, so if we had another package drink
:
或者库是否使用包,所以如果我们有另一个包drink
:
- milk.py
- juice.py
- 牛奶.py
- 果汁.py
contained in the package. The library
contains two packages?
包含在包中。在library
包含两个包?
Also, an application programming interface (API) usually contains a set of libraries is this at the top of the hierarchy:
此外,应用程序编程接口 (API) 通常包含一组位于层次结构顶部的库:
- API
- Library
- Package
- Module
- Script
- 应用程序接口
- 图书馆
- 包裹
- 模块
- 脚本
So an API will consist off all from 2-5?
那么一个 API 将包含 2-5 个?
采纳答案by Bakuriu
From The Python Tutorial - Modules
Module:
A module is a file containing Python definitions and statements. The file name is the module name with the suffix
.py
appended.Package:
Packages are a way of structuring Python's module namespace by using “dotted module names”.
模块:
模块是包含 Python 定义和语句的文件。文件名是
.py
附加后缀的模块名称。包:
包是一种通过使用“带点的模块名称”来构建 Python 模块命名空间的方法。
If you read the documentation for the import
statement gives more details, for example:
如果您阅读该import
声明的文档会提供更多详细信息,例如:
Python has only one type of module object, and all modules are of this type, regardless of whether the module is implemented in Python, C, or something else. To help organize modules and provide a naming hierarchy, Python has a concept of packages.
You can think of packages as the directories on a file system and modules as files within directories, but don't take this analogy too literally since packages and modules need not originate from the file system. For the purposes of this documentation, we'll use this convenient analogy of directories and files. Like file system directories, packages are organized hierarchically, and packages may themselves contain subpackages, as well as regular modules.
It's important to keep in mind that all packages are modules, but not all modules are packages. Or put another way, packages are just a special kind of module. Specifically, any module that contains a
__path__
attribute is considered a package.
Python只有一种类型的模块 object,所有模块都是这种类型,无论模块是用Python、C还是其他东西实现的。为了帮助组织模块并提供命名层次结构,Python 有一个包的概念。
您可以将包视为文件系统上的目录,将模块视为目录中的文件,但不要从字面上看这个类比,因为包和模块不需要源自文件系统。出于本文档的目的,我们将使用这种方便的目录和文件类比。与文件系统目录一样,包也是分层组织的,包本身可能包含子包以及常规模块。
重要的是要记住所有包都是模块,但并非所有模块都是包。或者换句话说,包只是一种特殊的模块。具体来说,任何包含
__path__
属性的模块都 被视为一个包。
Hence the term module
refers to a specific entity: it's a class whose instances are the module
objects you use in python programs. It is also used, by analogy, to refer to the file in the file system from which these instances "are created".
因此,该术语module
指的是特定实体:它是一个类,其实例是module
您在 Python 程序中使用的对象。以此类推,它也用于指代文件系统中的文件,从这些文件“创建”这些实例。
The term scriptis used to refer to a module whose aim is to be executed. It has the same meaning as "program" or "application", but it is usuallyused to describe simple and small programs(i.e. a single file with at most some hundreds of lines). Writing a script takes minutes or few hours.
术语脚本用于指代要执行其目标的模块。它与“程序”或“应用程序”具有相同的含义,但通常用于描述简单的小程序(即最多数百行的单个文件)。编写脚本需要几分钟或几个小时。
The term libraryis simply a generic term for a bunch of code that was designed with the aim of being usable by many applications. It provides some generic functionality that can be used by specific applications.
术语库只是一堆代码的通用术语,这些代码旨在被许多应用程序使用。它提供了一些可供特定应用程序使用的通用功能。
When a module/package/something else is "published" people often refer to it as a library. Often libraries contain a package or multiple related packages, but it could be even a single module.
当模块/包/其他东西被“发布”时,人们通常将其称为库。库通常包含一个包或多个相关的包,但它甚至可以是单个模块。
Libraries usually do not provide any specific functionality, i.e. you cannot "run a library".
库通常不提供任何特定功能,即您不能“运行库”。
The API can have different meanings depending on the context. For example:
API 可以根据上下文具有不同的含义。例如:
- it can define a protocol like the DB APIor the buffer protocol.
- it can define how to interact with an application(e.g. the
Python/C API
) - when related to a library/package it simply the interface provided by that library for its functionality(set of functions/classes/constants etc.)
- 它可以定义一个协议,如DB API或缓冲协议。
- 它可以定义如何与应用程序交互(例如
Python/C API
) - 当与库/包相关时,它只是该库为其功能提供的接口(函数集/类/常量等)
In any case an API is notpython code. It's a description which may be more or less formal.
在任何情况下,API都不是Python 代码。这是一个或多或少正式的描述。
回答by Kimvais
Only packageand modulehave a well-defined meaning specific to Python.
只有包和模块具有特定于 Python 的明确定义。
An APIis not a collection of code per se- it is more like a "protocol" specification how various parts (usually libraries) communicate with each other. There are a few notable "standard" APIs in python. E.g. the DB API
In my opinion, a libraryis anything that is not an application- in python, a library is a module- usually with submodules. The scope of a library is quite variable - for example the python standard libraryis vast (with quite a few submodules) while there are lots of single purpose libraries in the PyPi, e.g. a backport of
collections.OrderedDict
for py < 2.7A packageis a collection of python modules under a common namespace. In practice one is created by placing multiple python modules in a directory with a special
__init__.py
module (file).A moduleis a single file of python code that is meant to be imported. This is a bit of a simplification since in practice quite a few modules detect when they are run as scriptand do something special in that case.
A scriptis a single file of python code that is meant to be executedas the 'main' program.
If you have a set of code that spans multiple files, you probably have an applicationinstead of script.
一个API不是代码的集合本身-它更像一个“协议”说明书中的各个部件(通常库)如何与彼此通信。python中有一些值得注意的“标准”API。例如数据库 API
在我看来,库是任何不是应用程序的东西——在 python 中,库是一个模块——通常带有子模块。库的范围是相当可变的——例如,python 标准库很大(有很多子模块),而 PyPi 中有很多单一用途的库,例如for py < 2.7的反向移植
collections.OrderedDict
甲包是python模块在一个共同的命名空间的集合。在实践中,一个是通过将多个 python 模块放置在一个具有特殊
__init__.py
模块(文件)的目录中来创建的。甲模块是其旨在被Python代码的单个文件导入。这有点简化,因为实际上很多模块会检测它们何时作为脚本运行并在这种情况下做一些特殊的事情。
甲脚本是,是为了将Python代码的单个文件执行作为“主”程序。
如果您有一组跨越多个文件的代码,那么您可能有一个应用程序而不是脚本。
回答by Premraj
Library :It is a collection of modules.
库:它是模块的集合。
(Library either contains built in modules(written in C) + modules written in python).
(库要么包含内置模块(用 C 编写)+ 用 python 编写的模块)。
Module :Each of a set of standardized parts or independent units that can be used to construct a more complex structure.
模块:一组标准化部件或独立单元中的每一个,可用于构建更复杂的结构。
Speaking in informal language, A module is set of lines of code which are used for a specific purpose and can be used in other programs as it is , to avoid DRY(Don't Repeat Yourself) as a team and focusing on main requirement. source
在非正式的语言发言,A模块被设定,它被用于一个特定的目的,并且可以在其他程序中使用,因为它是行的代码,以避免DRY(don't řEPEAT Ÿ我们自己)作为一个团队和聚焦上主要要求。来源
APIis an interface for other applications to interact with your library without having direct access.
API是其他应用程序与您的库进行交互而无需直接访问的接口。
Packageis basically a directory with files.
包基本上是一个包含文件的目录。
Scriptmeans series of commands within a single file.
脚本是指单个文件中的一系列命令。
回答by Mizat
I will try to answer this without using terms the earliest of beginners would use,and explain why or how they used differently, along with the most "official" and/or most understood or uniform use of the terms.
我将尝试在不使用最早的初学者会使用的术语的情况下回答这个问题,并解释为什么或如何使用不同的术语,以及最“官方”和/或最容易理解或统一使用的术语。
It can be confusing, and I confused myself thinking to hard, so don't think to much about it. Anyways context matters, greatly.
这可能会让人感到困惑,而且我自己也很难思考,所以不要想太多。无论如何,上下文很重要。
Library- Most often will refer to the general library or another collection created with a similar format and use. The General Library is the sum of 'standard', popular and widely used Modules, witch can be thought of as single file tools, for now or short cuts making things possible or faster. The general library is an option most people enable when installing Python. Because it has this name "Python General Library" it is used often with similar structure, and ideas. Witch is simply to have a bunch of Modules, maybe even packages grouped together, usually in a list. The list is usually to download them. Generally it is just related files, with similar interests. That is the easiest way to describe it.
图书馆- 大多数情况下是指通用图书馆或其他以类似格式和用途创建的馆藏。通用库是“标准”、流行和广泛使用的模块的总和,可以将其视为单个文件工具,目前或使事情成为可能或更快的捷径。通用库是大多数人在安装 Python 时启用的选项。因为它有这个名称“Python 通用库”,所以它经常以类似的结构和想法使用。Witch 只是将一堆模块,甚至可能组合在一起的包,通常放在一个列表中。列表通常是下载它们。通常它只是相关文件,具有相似的兴趣。这是描述它的最简单方法。
Module- A Modulerefers to a file. The file has script 'in it' and the name of the file is the name of the module, Python files end with .py. All the file contains is code that ran together makes something happen, by using functions, strings ect. Main modules you probably see most often are popular because they are special modules that can get info from other files/modules. It is confusing because the name of the file and module are equal and just drop the .py. Really it's just code you can use as a shortcut written by somebody to make something easier or possible.
模块-模块指的是一个文件。该文件中包含脚本,文件名是模块的名称,Python 文件以 .py 结尾。文件包含的所有代码都是一起运行的,通过使用函数、字符串等使事情发生。您可能最常看到的主要模块很受欢迎,因为它们是可以从其他文件/模块获取信息的特殊模块。这是令人困惑的,因为文件和模块的名称是相同的,只是删除 .py。实际上,它只是您可以用作某人编写的快捷方式的代码,以使事情变得更容易或成为可能。
Package- This is a termis used to generally sometimes, although context makes a difference. The most common use from my experience is multiple modules (or files) that are grouped together. Why they are grouped together can be for a few reasons, that is when context matters. These are ways I have noticed the term package(s)used. They are a group of Downloaded, created and/or stored modules. Which can all be true, or only 1, but really it is just a file that references other files, that need to be in the correct structure or format, and that entire sum is the package itself, installed or may have been included in the python general library. A package can contain modules(.py files) because they depend on each other and sometimes may not work correctly, or at all. There is always a common goal of every part (module/file) of a package, and the total sum of all of the parts is the package itself.
包- 这是一个通常有时使用的术语,尽管上下文有所不同。根据我的经验,最常见的用途是将多个模块(或文件)组合在一起。为什么将它们组合在一起可能有几个原因,那就是上下文很重要。这些是我注意到术语包的方式用过的。它们是一组下载、创建和/或存储的模块。这可能都是真的,或者只有 1 个,但实际上它只是一个引用其他文件的文件,需要采用正确的结构或格式,并且整个总和是包本身、已安装或可能已包含在python通用库。一个包可以包含模块(.py 文件),因为它们相互依赖,有时可能无法正常工作,或者根本无法正常工作。一个包的每个部分(模块/文件)总是有一个共同的目标,所有部分的总和就是包本身。
Most often in Python Packages are Modules, because the package name is the name of the module that is used to connect all the pieces. So you can input a package because it is a module, also allows it to call upon other modules, that are not packages because they only perform a certain function, or task don't involve other files. Packages have a goal, and each module works together to achieve that final goal.
Python 包中最常见的是模块,因为包名称是用于连接所有部分的模块的名称。所以你可以输入一个包,因为它是一个模块,也允许它调用其他模块,这些模块不是包,因为它们只执行某个功能,或者任务不涉及其他文件。包有一个目标,每个模块协同工作以实现最终目标。
Most confusion come from a simple file file name or prefix to a file, used as the module name then again the package name.
大多数混淆来自一个简单的文件文件名或文件前缀,用作模块名,然后又是包名。
Remember Modules and Packages can be installed. Library is usually a generic term for listing, or formatting a group of modules and packages. Much like Pythons general library. A hierarchy would not work, APIs do not belong really, and if you did they could be anywhere and every ware involving Script, Module, and Packages, the worl library being such a general word, easily applied to many things, also makes API able to sit above or below that. Some Modules can be based off of other code, and that is the only time I think it would relate to a pure Python related discussion.
记住模块和包是可以安装的。库通常是列出或格式化一组模块和包的通用术语。很像 Python 的通用库。层次结构是行不通的,API 并不真正属于,如果你这样做了,它们可以在任何地方,涉及脚本、模块和包的所有软件,worl 库是一个如此笼统的词,很容易应用于许多事物,也使 API 成为可能坐在上面或下面。一些模块可以基于其他代码,这是我认为它与纯 Python 相关讨论相关的唯一一次。