什么是Python蛋?

2024-04-26 05:30:40 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

.egg文件是Python包的分发格式。它只是源代码发行版或Windows的替代品。但是请注意,对于纯的Python.egg文件是完全跨平台的。

.egg文件本身本质上是一个.zip文件。如果将扩展名更改为“zip”,则可以看到它将在存档中包含文件夹。

另外,如果您有一个.egg文件,则可以使用easy_install将其作为包安装

示例: 要为目录创建一个.egg文件,例如mymath,该目录本身可能有多个python脚本,请执行以下步骤:

# setup.py
from setuptools import setup, find_packages
setup(
    name = "mymath",
    version = "0.1",
    packages = find_packages()
    )

然后,从终端执行:

 $ python setup.py bdist_egg

这将产生很多输出,但当它完成后,您将看到您有三个新文件夹:builddistmymath.egg-info。我们只关心dist文件夹,您可以在其中找到带有默认python(安装)版本号的.egg文件,mymath-0.1-py3.5.egg(我的版本号是:3.5)

来源:Python library blog

Note: Egg packaging has been superseded by Wheel packaging.

与Java中的.jar文件的概念相同,它是一个.zip文件,其中一些元数据文件重命名为.egg,用于将代码作为包分发。

Specifically: The Internal Structure of Python Eggs

A "Python egg" is a logical structure embodying the release of a specific version of a Python project, comprising its code, resources, and metadata. There are multiple formats that can be used to physically encode a Python egg, and others can be developed. However, a key principle of Python eggs is that they should be discoverable and importable. That is, it should be possible for a Python application to easily and efficiently find out what eggs are present on a system, and to ensure that the desired eggs' contents are importable.

The .egg format is well-suited to distribution and the easy uninstallation or upgrades of code, since the project is essentially self-contained within a single directory or file, unmingled with any other projects' code or resources. It also makes it possible to have multiple versions of a project simultaneously installed, such that individual programs can select the versions they wish to use.

Python egs是将附加信息与Python项目捆绑在一起的一种方式,它允许在运行时检查和满足项目的依赖关系,并允许项目为其他项目提供插件。有几种二进制格式包含egg s,但最常见的是“.egg”zipfile格式,因为它是分发项目的方便格式。所有的格式都支持特定于包的数据、项目范围的元数据、C扩展和Python代码。

安装和使用PythonEggs的最简单方法是使用“Easy install”Python包管理器,它将为您查找、下载、构建和安装eggs;您只需告诉它要使用的Python项目的名称(以及可选的版本)。

PythonEggs可以与Python2.3及更高版本一起使用,也可以使用setuptools包构建(源代码请参见PythonSubversion沙盒,当前安装说明请参见EasyInstall页面)。

Python蛋的主要好处是:

  • 它们支持“轻松安装”Python包管理器之类的工具

  • .egg文件是Python包的“零安装”格式;不需要构建或安装步骤,只需将它们放在Python path或sys.path上并使用它们(如果使用C扩展名或数据文件,则可能需要安装运行时)

  • 它们可以包括包元数据,例如它们所依赖的其他鸡蛋

  • 它们允许将“名称空间包”(只包含其他包的包)拆分为单独的发行版(例如zope.,twisted)。,peak.*包可以作为单独的鸡蛋分发,而普通包必须始终放在同一个父目录下。这使得现在巨大的单片软件包可以作为单独的组件分发。

  • 它们允许应用程序或库指定库的所需版本,以便您可以在导入Twisted.Internet之前要求(“Twisted Internet>;=2.0”)。

  • 它们是将扩展或插件分发到可扩展应用程序和框架(如Trac,它使用egg s作为0.9b1版本的插件)的一种很好的格式,因为egg运行时提供了简单的api来定位eggs并找到它们的广告入口点(类似于Eclipse的“扩展点”概念)。

与Java的“jar”格式类似,标准化格式也可能带来其他好处。

相关问题 更多 >