pkg_resources:自己分配吗? [英] pkg_resources: get own distribution?

查看:111
本文介绍了pkg_resources:自己分配吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到文件的分发.该文件应发现其自己的分布.

I want to find the distribution of a file. The file should discovery its own distribution.

我尝试了这个,但这不起作用:

I tried this, but this does not work:

import os
import pkg_resources
for dist in pkg_resources.find_distributions(os.path.dirname(__file__)):
    print dist

具有上述的文件是使用pip install -e ...安装的.

The file with the of the above was installed using pip install -e ....

我在文档中找不到解决方案:

I could not find a solution in the docs:

https://pythonhosted.org/setuptools/pkg_resources.html#distribution-objects

解决方案不应包含软件包的字符串.它应该是通用的.

The solution should not contain the string of the package. It should be generic.

推荐答案

pkg_resources分发API需要分发名称;这不一定与当前"模块所使用的程序包名称相关.

The pkg_resources distribution APIs need a distribution name; this does not necessarily correlate with the package name the 'current' module is operating under.

例如,以BeautifulSoup项目为例.当前版本使用bs4作为软件包名称,但PyPI上的发行名称 beautifulsoup4 .

Take the BeautifulSoup project, for example. The current version uses bs4 as the package name, but the distribution name on PyPI is beautifulsoup4.

一个发行版也可以包含多个顶层程序包或模块;例如,pkg_resources模块本身就是setuptools发行版的一部分,它与setuptools软件包一起.

A distribution can contain more than one top-level package or module, too; the pkg_resources module itself is part of the setuptools distribution for example, together with the setuptools package.

这样,您不能概括并使用__file____package__,希望总是得到相应的分布.

As such, you cannot generalize and use __file__ or __package__ and hope to always get the corresponding distribution.

如果您的发行版是作为鸡蛋安装的(例如,使用easy_installzc.buildout),则可以在路径中搜索.egg扩展名,在该位置拆分路径并使用 Distribution.from_filename() 从中产生一个Distribution对象;" http://setuptools.readthedocs.io/zh-CN/latest/pkg_resources.html#getting-or-creating-distributions"rel =" noreferrer> Distribution.from_filename() ;文件名包含所有必要的信息.但是,如果您的软件包是使用pip安装的,则会创建一个单独的.egg-info文件,并且您将返回第一个文件(无法找到发行名称).

If your distribution was installed as an egg (using easy_install or zc.buildout for example), then you can search for the .egg extensions in the path, split the path at that location and use Distribution.from_filename() to produce a Distribution object from that; the filename contains all the information necessary. If your package was installed using pip however, a separate .egg-info file is created instead and you are back to square one (no distribution name locatable).

相反,您可以使用 pkg_resources.Environment()实例;但您会遇到将现有发行版与给定模块或包匹配的同样问题,至少不能与公共API匹配.您可以扫描所有分布,.location属性(用于鸡蛋)和(可能)(私有)Distribution._provider属性以及(完全可选)installed-files.txt元数据条目的组合(如果有的话,它包含相对路径(!))可以让您扫描整个环境以查找匹配的分布,但这完全取决于各种

In the other direction, you can get all Distribution objects for your environment with a pkg_resources.Environment() instance; but you'll run into the same problems matching existing distributions to a given module or package, at least not with the public API. You could scan all distributions, the combination of the .location attribute (for eggs) and perhaps the (private) Distribution._provider attribute together with (entirely optional) presence of a installed-files.txt metadata entry (which if is available, contains relative paths(!)) would let you scan your whole environment for a matching distribution, but this is entirely dependent on the internal implementation details of the various resource providers. And it could potentially be an expensive search, certainly for larger installations.

因此,最好的选择是仅使用硬编码的分发名称.

As such, your best option is to just use a hardcoded distribution name.

这篇关于pkg_resources:自己分配吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆