Pluggable Distributions of Python Software
==========================================
Distributions
-------------
A "Distribution" is a collection of files that represent a "Release" of a
"Project" as of a particular point in time, denoted by a
"Version"::
>>> import sys, pkg_resources
>>> from pkg_resources import Distribution
>>> Distribution(project_name="Foo", version="1.2")
Foo 1.2
Distributions have a location, which can be a filename, URL, or really anything
else you care to use::
>>> dist = Distribution(
... location="http://example.com/something",
... project_name="Bar", version="0.9"
... )
>>> dist
Bar 0.9 (http://example.com/something)
Distributions have various introspectable attributes::
>>> dist.location
'http://example.com/something'
>>> dist.project_name
'Bar'
>>> dist.version
'0.9'
>>> dist.py_version == '{}.{}'.format(*sys.version_info)
True
>>> print(dist.platform)
None
Including various computed attributes::
>>> from pkg_resources import parse_version
>>> dist.parsed_version == parse_version(dist.version)
True
>>> dist.key # case-insensitive form of the project name
'bar'
Distributions are compared (and hashed) by version first::
>>> Distribution(version='1.0') == Distribution(version='1.0')
True
>>> Distribution(version='1.0') == Distribution(version='1.1')
False
>>> Distribution(version='1.0') < Distribution(version='1.1')
True
but also by project name (case-insensitive), platform, Python version,
location, etc.::
>>> Distribution(project_name="Foo",version="1.0") == \
... Distribution(project_name="Foo",version="1.0")
True
>>> Distribution(project_name="Foo",version="1.0") == \
... Distribution(project_name="foo",version="1.0")
True
>>> Distribution(project_name="Foo",version="1.0") == \
... Distribution(project_name="Foo",version="1.1")
False
>>> Distribution(project_name="Foo",py_version="2.3",version="1.0") == \
... Distribution(project_name="Foo",py_version="2.4",version="1.0")
False
>>> Distribution(location="spam",version="1.0") == \
... Distribution(location="spam",version="1.0")
True
>>> Distribution(location="spam",version="1.0") == \
... Distribution(location="baz",version="1.0")
False
Hash and compare distribution by prio/plat
Get version from metadata
provider capabilities
egg_name()
as_requirement()
from_location, from_filename (w/path normalization)
Releases may have zero or more "Requirements", which indicate
what releases of another project the release requires in order to
function. A Requirement names the other project, expresses some criteria
as to what releases of that project are acceptable, and lists any "Extras"
that the requiring release may need from that project. (An Extra is an
optional feature of a Release, that can only be used if its additional
Requirements are satisfied.)
The Working Set
---------------
A collection of active distributions is called a Working Set. Note that a
Working Set can contain any importable distribution, not just pluggable ones.
For example, the Python standard library is an importable distribution that
will usually be part of the Working Set, even though it is not pluggable.
Similarly, when you are doing development work on a project, the files you are
editing are also a Distribution. (And, with a little attention to the
directory names used, and including some additional metadata, such a
"development distribution" can be made pluggable as well.)
>>> from pkg_resources import WorkingSet
A working set's entries are the sys.path entries that correspond to the active
distributions. By default, the working set's entries are the items on
``sys.path``::
>>> ws = WorkingSet()
>>> ws.entries == sys.path
True
But you can also create an empty working set explicitly, and add distributions
to it::
>>> ws = WorkingSet([])
>>> ws.add(dist)
>>> ws.entries
['http://example.com/something']
>>> dist in ws
True
>>> Distribution('foo',version="") in ws
False
And you can iterate over its distributions::
>>> list(ws)
[Bar 0.9 (http://example.com/something)]
Adding the same distribution more than once is a no-op::
>>> ws.add(dist)
>>> list(ws)
[Bar 0.9 (http://example.com/something)]
For that matter, adding multiple distributions for the same project also does
nothing, because a working set can only hold one active distribution per
project -- the first one added to it::
>>> ws.add(
... Distribution(
... 'http://example.com/something', project_name="Bar",
... version="7.2"
... )
... )
>>> list(ws)
[Bar 0.9 (http://example.com/something)]
You can append a path entry to a working set using ``add_entry()``::
>>> ws.entries
['http://example.com/something']
>>> ws.add_entry(pkg_resources.__file__)
>>> ws.entries
['http://example.com/something', '...pkg_resources...']
Multiple additions result in multiple entries, even if the entry is already in
the working set (because ``sys.path`` can contain the same entry more than
once)::
>>> ws.add_entry(pkg_resources.__file__)
>>> ws.entries
['...example.com...', '...pkg_resources...', '...pkg_resources...']
And you can specify the path entry a distribution was found under, using the
optional second parameter to ``add()``::
>>> ws = WorkingSet([])
>>> ws.add(dist,"foo")
>>> ws.entries
['foo']
But even if a distribution is found under multiple path entries, it still only
shows up once when iterating the working set:
>>> ws.add_entry(ws.entries[0])
>>> list(ws)
[Bar 0.9 (http://example.com/something)]
You can ask a WorkingSet to ``find()`` a distribution matching a requirement::
>>> from pkg_resources import Requirement
>>> print(ws.find(Requirement.parse("Foo==1.0"))) # no match, return None
None
>>> ws.find(Requirement.parse("Bar==0.9")) # match, return distribution
Bar 0.9 (http://example.com/something)
Note that asking for a conflicting version of a distribution already in a
working set triggers a ``pkg_resources.VersionConflict`` error:
>>> try:
... ws.find(Requirement.parse("Bar==1.0"))
... except pkg_resources.VersionConflict as exc:
... print(str(exc))
... else:
... raise AssertionError("VersionConflict was not raised")
(Bar 0.9 (http://example.com/something), Requirement.parse('Bar==1.0'))
You can subscribe a callback function to receive notifications whenever a new
distribution is added to a working set. The callback is immediately invoked
once for each existing distribution in the working set, and then is called
again for new distributions added thereafter::
>>> def added(dist): print("Added %s" % dist)
>>> ws.subscribe(added)
Added Bar 0.9
>>> foo12 = Distribution(project_name="Foo", version="1.2", location="f12")
>>> ws.add(foo12)
Added Foo 1.2
Note, however, that only the first distribution added for a given project name
will trigger a callback, even during the initial ``subscribe()`` callback::
>>> foo14 = Distribution(project_name="Foo", version="1.4", location="f14")
>>> ws.add(foo14) # no callback, because Foo 1.2 is already active
>>> ws = WorkingSet([])
>>> ws.add(foo12)
>>> ws.add(foo14)
>>> ws.subscribe(added)
Added Foo 1.2
And adding a callback more than once has no effect, either::
>>> ws.subscribe(added) # no callbacks
# and no double-callbacks on subsequent additions, either
>>> just_a_test = Distribution(project_name="JustATest", version="0.99")
>>> ws.add(just_a_test)
Added JustATest 0.99
Finding Plugins
---------------
``WorkingSet`` objects can be used to figure out what plugins in an
``Environment`` can be loaded without any resolution
没有合适的资源?快使用搜索试试~ 我知道了~
setuptools-51.1.0.tar.gz
0 下载量 23 浏览量
2024-05-12
23:19:24
上传
评论
收藏 1.96MB GZ 举报
温馨提示
Python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。Python社区提供了大量的第三方库,如NumPy、Pandas和Requests,极大地丰富了Python的应用领域,从数据科学到Web开发。Python库的丰富性是Python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,Matplotlib和Seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。
资源推荐
资源详情
资源评论
收起资源包目录
setuptools-51.1.0.tar.gz (334个子文件)
launcher.c 10KB
setup.cfg 2KB
setup.cfg 0B
msvc-build-launcher.cmd 2KB
theme.conf 71B
pygments.css 3KB
nature.css_t 4KB
my_test_package-1.0-py3.7.egg 843B
wininst-14.0-amd64.exe 574KB
wininst-14.0.exe 448KB
wininst-9.0-amd64.exe 219KB
wininst-10.0-amd64.exe 217KB
wininst-9.0.exe 192KB
wininst-10.0.exe 187KB
gui-64.exe 74KB
cli-64.exe 73KB
cli-32.exe 64KB
cli.exe 64KB
gui-32.exe 64KB
gui.exe 64KB
wininst-7.1.exe 64KB
wininst-6.0.exe 60KB
wininst-8.0.exe 60KB
.gitignore 0B
indexsidebar.html 534B
index.html 174B
external.html 92B
MANIFEST.in 613B
tox.ini 2KB
pytest.ini 1KB
LICENSE 1KB
PKG-INFO 4KB
PKG-INFO 4KB
PKG-INFO 187B
pyparsing.py 227KB
pyparsing.py 227KB
__init__.py 105KB
easy_install.py 84KB
msvc.py 50KB
dist.py 49KB
ccompiler.py 46KB
package_index.py 40KB
test_easy_install.py 39KB
dist.py 38KB
bdist_msi.py 35KB
specifiers.py 31KB
specifiers.py 31KB
build_ext.py 31KB
test_egg_info.py 30KB
test_resources.py 30KB
msvc9compiler.py 30KB
test_config.py 27KB
install.py 27KB
egg_info.py 25KB
appdirs.py 24KB
tags.py 24KB
tags.py 24KB
msvccompiler.py 23KB
bdist_rpm.py 21KB
config.py 21KB
sysconfig.py 21KB
util.py 20KB
_msvccompiler.py 20KB
test_build_ext.py 20KB
test_dist.py 19KB
sdist.py 19KB
bdist_egg.py 18KB
cmd.py 18KB
test_manifest.py 18KB
fancy_getopt.py 17KB
build_py.py 17KB
test_sdist.py 17KB
cygwinccompiler.py 16KB
bdist_wininst.py 16KB
test_wheel.py 15KB
version.py 15KB
version.py 15KB
ordered_set.py 15KB
test_build_meta.py 15KB
bcppcompiler.py 15KB
unixccompiler.py 14KB
test_sdist.py 14KB
test_archive_util.py 14KB
sandbox.py 14KB
test_pkg_resources.py 14KB
config.py 13KB
build_ext.py 13KB
filelist.py 13KB
version.py 12KB
text_file.py 12KB
register.py 11KB
test_util.py 11KB
test_filelist.py 11KB
test_sysconfig.py 11KB
extension.py 10KB
test_dist.py 10KB
test_packageindex.py 10KB
build_meta.py 10KB
test_register.py 10KB
markers.py 9KB
共 334 条
- 1
- 2
- 3
- 4
资源评论
程序员Chino的日记
- 粉丝: 3674
- 资源: 5万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 系统学习linux命令
- java毕业设计-基于SSM的党务政务服务热线平台【代码+论文+PPT】.zip
- YOLOv3 在 GPU 上使用自己的数据进行训练 YOLOv3 的 Keras 实现.zip
- YOLOv3 和 YOLOv3-tiny 的 Tensorflow js 实现.zip
- 石头剪刀布-YOLOV7标记的数据集
- YOLOV3 pytorch 实现为 python 包.zip
- 石头剪刀布-YOLOV8标记的数据集
- YOLOv2 在 TF,Keras 中的实现 允许在不同的特征检测器(MobileNet、Darknet-19)上进行实验 论文.zip
- 石头剪刀布-YOLOV11标记的数据集
- YoloV1的tensorflow实现.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功