简体   繁体   English

如何获取 Python 分发 URL?

[英]How do I get a Python distribution URL?

In their setup.py Python packages provides some information.在他们的 setup.py Python 包中提供了一些信息。 This information can then be found in the PKG_INFO file of the egg.然后可以在鸡蛋的 PKG_INFO 文件中找到此信息。

How can I access them once I have installed the package?安装软件包后如何访问它们?

For instance, if I have the following module:例如,如果我有以下模块:

setup(name='myproject',
      version='1.2.0.dev0',
      description='Demo of a setup.py file.',
      long_description=README + "\n\n" + CHANGELOG + "\n\n" + CONTRIBUTORS,
      license='Apache License (2.0)',
      classifiers=[
          "Programming Language :: Python",
          "Programming Language :: Python :: 2",
          "Programming Language :: Python :: 2.7",
          "Programming Language :: Python :: 3",
          "Programming Language :: Python :: 3.4",
          "Programming Language :: Python :: 3.5",
          "Programming Language :: Python :: Implementation :: CPython",
          "Programming Language :: Python :: Implementation :: PyPy",
          "Topic :: Internet :: WWW/HTTP",
          "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
          "License :: OSI Approved :: Apache Software License"
      ],
      keywords="web sync json storage services",
      url='https://github.com/Kinto/kinto')

How can I use Python to get back the information provided in the setup.py?如何使用 Python 取回 setup.py 中提供的信息?

I was thinking of something similar to that:我在想类似的事情:

import pkg_resource
url = pkg_resource.get_distribution(__package__).url

Any idea?有什么想法吗?

There is apparently a private API that let you do that with pkg_resources :显然有一个私有 API 可以让您使用pkg_resources做到这pkg_resources

import pkg_resources
d = pkg_resources.get_distribution(__package__)
metadata = d._get_metadata(d.PKG_INFO)
home_page = [m for m in metadata if m.startswith('Home-page:')]
url = home_page[0].split(':', 1)[1].strip()

I wish we could do better.我希望我们能做得更好。

I wish we could do better.我希望我们能做得更好。

Actually, we can.事实上,我们可以。 There is no need to use the private method, we might just do:不需要使用私有方法,我们可以这样做:

import pkg_resources
import distutils
import io

distribution = pkg_resources.get_distribution(__package__)
metadata_str = distribution.get_metadata(distribution.PKG_INFO)
metadata_obj = distutils.dist.DistributionMetadata()
metadata_obj.read_pkg_file(io.StringIO(metadata_str))
url = metadata_obj.url

Starting from python 3.8, you can use importlib.metadata to extract metadata of a package.从 python 3.8 开始,您可以使用importlib.metadata来提取包的元数据。

For example, to extract metadata of urllib3 :例如,要提取urllib3元数据:

>>> from importlib import metadata
>>> import urllib3
>>> list(metadata.metadata('urllib3'))
['Metadata-Version', 'Name', 'Version', 'Summary', 'Home-page', 'Author', 'Author-email', 'License', 'Project-URL', 'Project-URL', 'Project-URL', 'Description', 'Keywords', 'Platform', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Classifier', 'Requires-Python', 'Provides-Extra', 'Provides-Extra', 'Provides-Extra']
>>> metadata.metadata('urllib3')['Version']
'1.25.8'
>>> metadata.metadata('urllib3')['Project-URL']
'Documentation, https://urllib3.readthedocs.io/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM