Home Backend Development Python Tutorial 解析Python编程中的包结构

解析Python编程中的包结构

Jun 10, 2016 pm 03:07 PM
python Bag

假设你想设计一个模块集(也就是一个“包”)来统一处理声音文件和声音数据。通常由它们的扩展有不同的声音格式,例如:WAV,AIFF,AU),所以你可能需要创建和维护一个不断增长的各种文件格式之间的转换的模块集合。并且可能要执行声音数据处理(如混合,添加回声,应用平衡功能),所以你写一个永无止境的流模块来执行这些操作:模块设计的包如下:

sound/             Top-level package
   __init__.py        Initialize the sound package
   formats/         Subpackage for file format conversions
       __init__.py
       wavread.py
       wavwrite.py
       aiffread.py
       aiffwrite.py
       auread.py
       auwrite.py
       ...
   effects/         Subpackage for sound effects
       __init__.py
       echo.py
       surround.py
       reverse.py
       ...
   filters/         Subpackage for filters
       __init__.py
       equalizer.py
       vocoder.py
       karaoke.py
       ...
Copy after login

当导入包以后,Python通过sys.path中的目录来寻找包的子目录。 每一个包都必须有__init__.py文件,这样做是为了防止某些目录有一个共同的名字。在最简单的情况下,__ init__.py可以只是一个空文件,但它也可以执行包的初始化代码,包括设置__all__变量,稍后介绍。 我们可以从包中导入单个模块,

例如: import sound.effects.echo 这会载入子模块sound.effects.echo。它必须引用全名。

sound.effects.echo.echofilter(input, output, delay=0.7, atten=4)
Copy after login

另外一种导入子模块的方法: from sound.effects import echo 这样就加载了echo子模块,没有包括包的前缀,因此它可以用作如下:

echo.echofilter(input, output, delay=0.7, atten=4)
Copy after login

或者可以

from sound.effects.echo import echofilter echofilter(input, output, delay=0.7, atten=4)
Copy after login

请注意,如果你使用包导入一个子模块(或子包),像一个函数,类或变量。 import语句首先测试导入的对象是否包中定义,如果没有,它假定这是一个模块,并尝试加载它。如果还没有找到,则会引发一个ImportError异常。

python 包管理工具大乱斗
1. distutils

distutils 是 python 标准库的一部分,2000年发布。使用它能够进行 python 模块的 安装 和 发布。

setup.py 就是利用 distutils 的功能写成,我们可以看一个简单的 setup.py 的例子。

在这里可以看到关于 setupt.py 格式的所有详细描述:Writing the Setup Script。

要安装一个模块到当前的 python 环境中,可以使用这个模块提供的 setup.py 文件:

python setup.py install

Copy after login

下面的代码会发布一个 python 模块,将其打包成 tar.gz 或者 zip 压缩包:

python setup.py sdist
Copy after login

甚至能打包成 rpm 或者 exe 安装包:

python setup.py bdist_rpm
python setup.py bdist_wininst
Copy after login

2. setuptools 和 distribute

setuptools 是一个为了增强 distutils 而开发的集合,2004年发布。它包含了 easy_install 这个工具。

ez_setup.py 是 setuptools 的安装工具。ez 就是 easy 的缩写。

简单的说,setuptools 是一个项目的名称,是基础组件。而 easy_install 是这个项目中提供的工具,它依赖基础组件工作。

为了方便描述,下面文章中提到的 setuptools 被认为与 easy_install 同义。

使用 setuptools 可以自动 下载、构建、安装和管理 python 模块。

例如,从 PyPI 上安装一个包:

easy_install SQLObject
Copy after login

下载一个包文件,然后安装它:

easy_install http://example.com/path/to/MyPackage-1.2.3.tgz
Copy after login

从一个 .egg 格式安装:

easy_install /my_downloads/OtherPackage-3.2.1-py2.3.egg
Copy after login

distribute 是 setuptools 的一个分支版本。分支的原因可能是有一部分开发者认为 setuptools 开发太慢了。但现在,distribute 又合并回了 setuptools 中。因此,我们可以认为它们是同一个东西。事实上,如果你查看一下 easy_install 的版本,会发现它本质上就是 distribute 。

# easy_install --version
distribute 0.6.28
Copy after login

3. Eggs

Eggs 格式是 setuptools 引入的一种文件格式,它使用 .egg 扩展名,用于 Python 模块的安装。

setuptools 可以识别这种格式。并解析它,安装它。

4. pip

注意,从此处开始,easy_install 和 setuptools 不再同义。

pip 是目前 python 包管理的事实标准,2008年发布。它被用作 easy_install 的替代品,但是它仍有大量的功能建立在 setuptools 组件之上。

pip 希望不再使用 Eggs 格式(虽然它支持 Eggs),而更希望采用“源码发行版”(使用 python setup.py sdict 创建)。这可以充分利用 Requirements File Format 提供的方便功能。

pip 可以利用 requirments.txt 来实现在依赖的安装。在 setup.py 中,也存在一个 install_requires 表来指定依赖的安装。

pip 支持 git/svn/hg 等流行的 VCS 系统,可以直接从 gz 或者 zip 压缩包安装,支持搜索包,以及指定服务器安装等等功能。

pip vs easy_install 详细介绍了两者的不同。它们可以说是各占胜场,但 pip 明显优势更大。

5. wheel

wheel 本质上是一个 zip 包格式,它使用 .whl 扩展名,用于 python 模块的安装,它的出现是为了替代 Eggs。

wheel 还提供了一个 bdist_wheel 作为 setuptools 的扩展命令,这个命令可以用来生成 wheel 包。

pip 提供了一个 wheel 子命令来安装 wheel 包。当然,需要先安装 wheel 模块。

setup.cfg 可以用来定义 wheel 打包时候的相关信息。

Wheel vs Egg 详细介绍了 wheel 和 Eggs 格式的区别,很显然,wheel 优势明显。

Python Wheels 网站展示了使用 Wheels 发行的 python 模块在 PyPI 上的占有率。

pypip.in 也支持 wheel。

6. distutils2 和 distlib

distutils2 被设计为 distutils 的替代品。从2009年开发到2012年。它包含更多的功能,并希望以 packaging 作为名称进入 python 3.3 成为标准库的一部分。但这个计划 后来停滞了 。

distlib 是 distutils2 的部分,它为 distutils2/packaging 提供的低级功能增加高级 API,使其便于使用。

这里 介绍了 distlib 没有进入 python 3.3 标准库的一些原因。

因此,可以暂时不必了解这两个工具,静观其变即可。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1663
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles