python包
Today we are going to learn about Python Package. Before proceeding to this tutorial you should have knowledge about Python Modules which you can find it here.
今天,我们将学习Python包。 在继续本教程之前,您应该了解有关Python模块的知识,您可以在这里找到它。
Python包 (Python Package)
Python package is a collection of modules in directories that give a package hierarchy. More elaborately, python packages are a way of structuring python’s module by using “dotted module names”. So A.B actually indicates that B is a sub module which is under a package named A.
Python软件包是目录中提供软件包层次结构的模块的集合。 更详细地说,python包是通过使用“点分模块名称”来构造python模块的一种方式。 因此,AB实际上表明B是一个子模块,位于名为A的程序包下。
So as modules are way of handling functions and namespace in a better way, in that way python package is the way of handling two or more modules in a structured method.
因此,由于模块是更好地处理函数和名称空间的方式,因此python包是在结构化方法中处理两个或多个模块的方式。
Suppose you want to design a collection of modules for handling the music files. Take a look at the following structures-
假设您要设计用于处理音乐文件的模块集合。 看一下以下结构-
music/ Top-level package__init__.py Initialize the music packageformats/ Subpackage for file format conversions__init__.pywavread.pywavwrite.pyaiffread.pyaiffwrite.pyauread.pyauwrite.py...effects/ Subpackage for sound effects__init__.pyecho.pysurround.pyreverse.py...filters/ Subpackage for filters__init__.pyequalizer.pyvocoder.pykaraoke.py...
Every python package needs to have a __init__.py
file, which will ensure that this directory will be treated as a python package. Generally __init__.py
can be just an empty file or it can also be an executable initialisation code for the package or set the __all__
variable which will be explored in the later part of this tutorial.
每个python软件包都需要有一个__init__.py
文件,这将确保该目录将被视为python软件包。 通常__init__.py
可以是一个空文件,也可以是该程序包的可执行初始化代码,也可以设置__all__
变量,这将在本教程的后面部分进行探讨。
To import individual module from the package one can use any of the following ways.
要从包中导入单个模块,可以使用以下任何一种方法。
import music.formats.wavwrite
Or,
要么,
from music.formats import wavwrite
The above commands load the sub module music.formats.wavwrite
. Of course this must be referenced with it’s full name. Suppose module wavwrite.py
has a functions named writeFile(aFileName)
which takes name of a file as it’s argument. So to call it we have to write like this;
上面的命令将加载子模块music.formats.wavwrite
。 当然,必须使用全名进行引用。 假设模块wavwrite.py
具有一个名为writeFile(aFileName)
的函数,该函数以文件名作为参数。 因此,我们必须这样写:
import music.formats.wavwrite
...
...music.formats.wavwrite.writeFile(outputFileName)
Or, in the second way-
或者,第二种方式
from music.formats import wavwrite
...
...
wavwrite.writeFile(outputFileName)
Also one may wonder, there is also another variation to import the desired function or variable directly;
还可能有人想知道,还有另一种变体可以直接导入所需的函数或变量。
from music.formats.wavwrite import writeFile
...
...
writeFile(outputFileName)
我可以从Python包中导入* (Can I import * From a Python Package)
A curious reader may wonder what if one writes from music.formats import *
like we did while importing from a module. Well this might cause unwanted side-effects and also this will consume a lot of time.
一个好奇的读者可能会想,如果from music.formats import *
写入内容from music.formats import *
就像我们从模块导入时所做的那样,该怎么from music.formats import *
? 好吧,这可能会导致不良的副作用,并且还会消耗大量时间。
Ideal solution to do this would be if the package author provide an explicit index of the package. If a package’s __init__.py
code defines a list named __all__
, it would be considered as the index of module names that should be imported when from music.formats import *
is encountered.
这样做的理想解决方案是,如果程序包作者提供了程序包的显式索引。 如果包的__init__.py
代码定义了名为__all__
的列表,则将其视为遇到from music.formats import *
的模块名称的索引。
Let’s see a different example to understand this concept clearly. Suppose we have a package structure like this;
让我们看一个不同的例子,以清楚地理解这个概念。 假设我们有一个这样的包结构:
Here you can see under \music there is a __init__.py
. If the __all__
is defined below;
在这里,您可以在\ music下看到一个__init__.py
。 如果__all__
定义如下;
__all__ = ["admin", "apps", "models"]
Then only sub modules enlisted in the above list will imported while a from music import *
is encountered. The rest of the sub modules and variables will be ignored.
然后,仅遇到上述列表中列出的子模块,而遇到from music import *
子模块。 其余子模块和变量将被忽略。
If __all__
is not defined, there is guarantee that all the submodules under it will be imported. The statement from music import *
only ensures that music package has been imported.
如果未定义__all__
,则可以确保将其下的所有子模块都导入。 from music import *
的语句仅确保已导入音乐包。
So that’s pretty much all of the basic information about python package. For more information you can see the official python doc – https://docs.python.org/3/tutorial/modules.html#packages
因此,这几乎是有关python包的所有基本信息。 有关更多信息,请参见官方python文档– https://docs.python.org/3/tutorial/modules.html#packages
So keep practicing. #happy_coding 🙂
所以继续练习。 #happy_coding🙂
翻译自: https://www.journaldev.com/14339/python-package
python包