[英]How can I get the author name, project description etc from a Distribution object in pkg_resources?
pkg_resources api let's you get Distribution objects that represent egg distributions inside a directory. pkg_resources api让你获得代表目录中egg分配的Distribution对象。 I can trivially get the project name and version of the distribution with dist.project_name
and dist.version
, however I'm lost on how I can get the other metadata that's usually specified in the setup script (like author, description, etc.) 我可以通过dist.project_name
和dist.version
分发的项目名称和版本,但是我失去了如何获取通常在安装脚本中指定的其他元数据(如作者,描述等)
I was looking how to do the same thing. 我正在寻找如何做同样的事情。 This is what I came up with. 这就是我提出的。 It is probably not the best solution, but seems to work. 它可能不是最好的解决方案,但似乎有效。
# get the raw PKG-INFO data
from pkg_resources import get_distribution
pkgInfo = get_distribution('myapp').get_metadata('PKG-INFO')
# parse it using email.Parser
from email import message_from_string
msg = message_from_string(pkgInfo)
print(msg.items())
# optional: convert it to a MultiDict
from webob.multidict import MultiDict
metadata = MultiDict(msg)
print(metadata.get('Author'))
print(metadata.getall('Classifier'))
Same problem here. 同样的问题在这里 I solved it by creating a file package.py
under mymodule
with all the variables that would otherwise be hardcoded into setup.py
. 我通过在mymodule
下创建一个包含所有变量的文件package.py
解决了这个问题,否则这些变量将被硬编码到setup.py
。 Then in setup.py
: 然后在setup.py
:
pkg_info = get_package_info('mymodule')
setup(**pkg_info)
However in setup.py
, I cannot directly import mymodule.package as pkg_info
because that would cause setup.py to require the module it is defining. 但是在setup.py
,我无法直接import mymodule.package as pkg_info
因为这会导致setup.py需要它正在定义的模块。 Therefore a custom import method get_package_info(module_name)
is needed in setup.py
: 因此, setup.py
需要自定义导入方法get_package_info(module_name)
:
def get_package_info(module_name):
'''
Read info about package version, author etc
from <module_name>/package.py
Return:
dict {'version': '1.2.3', 'author': 'John Doe', ...}
'''
here = os.path.dirname(os.path.abspath(__file__))
code_globals = {}
code_locals = {}
with open(os.path.join(here, module_name, 'package.py')) as f:
code = compile(f.read(), "package.py", 'exec')
exec(code, code_globals, code_locals)
return code_locals
The mymodule/package.py
: mymodule/package.py
:
# -*- coding: utf-8 -*-
name = 'mymodule'
version = '1.2.3'
description = 'My module for demonstration'
url = 'https://github.com/myorg/mymodule'
author = 'John Doe'
author_email = 'john.doe@example.com'
...
packages = ['mymodule']
install_requires = ['myotherpackage', 'somepackage']
...
With this method I can easily access the package info anywhere where I can access the module, eg: from mymodule.package import version, author
. 使用这种方法,我可以在任何可以访问模块的地方轻松访问包信息,例如: from mymodule.package import version, author
。 Also, the info needs to be stored only in one place. 此外,信息只需存储在一个地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.