您可以使用来获取python发行版的版本
import pkg_resources
pkg_resources.get_distribution("distro").version
如果您知道发行版名称,这很好,但是我需要在运行时动态找出我的发行版名称。
# Common framework base app class, extended by each app
class App(object):
def get_app_version(self) -> str:
package_name = self.__class__.__module__.split('.')[0]
try:
return pkg_resources.get_distribution(package_name).version
except Exception:
return "development"
此功能适用于应用程序的程序包名称与分发名称相同(例如requests
)的情况。但是,一旦它们不匹配(例如my-app
包含软件包my_app
),此操作将失败。
所以我需要的是发行版及其软件包之间的映射,我确定该映射必须存在于某个地方,因为pip似乎知道在调用卸载时要删除的内容:
$ pip uninstall requests
Uninstalling requests-2.21.0:
Would remove:
/home/user/.virtualenvs/app/lib/python3.6/site-packages/requests-2.21.0.dist-info/*
/home/user/.virtualenvs/app/lib/python3.6/site-packages/requests/*
如何以编程方式访问此映射?
答案 0 :(得分:0)
经过两个小时的探索pkg_resources
并阅读了source for pip's uninstall,我的工作如下:
import inspect
import pkg_resources
import csv
class App(object):
def get_app_version(self) -> str:
# Iterate through all installed packages and try to find one that has the app's file in it
app_def_path = inspect.getfile(self.__class__)
for dist in pkg_resources.working_set:
try:
filenames = [
os.path.normpath(os.path.join(dist.location, r[0]))
for r in csv.reader(dist.get_metadata_lines("RECORD"))
]
if app_def_path in filenames:
return dist.version
except FileNotFoundError:
# Not pip installed or something
pass
return "development"
这将遍历所有已安装的软件包,并针对每个遍历的文件列表进行遍历,并尝试将其与当前文件进行匹配,从而将软件包与发行版进行匹配。这不是很理想,我仍然愿意寻求更好的答案。
答案 1 :(得分:0)
如果您正在寻找一种既可以在开发中使用的解决方案,也可以在未安装或仅在本地调用的版本中使用,请尝试此解决方案。
进口:
import ast
import csv
import inspect
from os import listdir, path
import pkg_resources
实用功能:
def get_first_setup_py(cur_dir):
if 'setup.py' in listdir(cur_dir):
return path.join(cur_dir, 'setup.py')
prev_dir = cur_dir
cur_dir = path.realpath(path.dirname(cur_dir))
if prev_dir == cur_dir:
raise StopIteration()
return get_first_setup_py(cur_dir)
现在使用Python的ast
库:
def parse_package_name_from_setup_py(setup_py_file_name):
with open(setup_py_file_name, 'rt') as f:
parsed_setup_py = ast.parse(f.read(), 'setup.py')
# Assumes you have an `if __name__ == '__main__':`, and that it's at the end:
main_body = next(sym for sym in parsed_setup_py.body[::-1]
if isinstance(sym, ast.If)).body
setup_call = next(sym.value
for sym in main_body[::-1]
if isinstance(sym, ast.Expr) and
isinstance(sym.value, ast.Call) and
sym.value.func.id in frozenset(('setup',
'distutils.core.setup',
'setuptools.setup')))
package_version = next(keyword
for keyword in setup_call.keywords
if keyword.arg == 'version'
and isinstance(keyword.value, ast.Name))
# Return the raw string if it is one
if isinstance(package_version.value, ast.Str):
return package_version.s
# Otherwise it's a variable at the top of the `if __name__ == '__main__'` block
elif isinstance(package_version.value, ast.Name):
return next(sym.value.s
for sym in main_body
if isinstance(sym, ast.Assign)
and isinstance(sym.value, ast.Str)
and any(target.id == package_version.value.id
for target in sym.targets)
)
else:
raise NotImplemented('Package version extraction only built for raw strings and '
'variables in the same function that setup() is called')
最后通过将return "development"
更改为:来替换@Gricey答案中的函数:
return parse_package_name_from_setup_py(get_first_setup_py(path.dirname(__file__)))
答案 2 :(得分:0)
我相信,如果可能的话,该项目的名称应使用硬编码。如果没有,那么类似以下的功能可以帮助找出包含当前文件(__file__
)的已安装发行版的元数据:
import pathlib
import importlib_metadata
def get_project_distribution():
for dist in importlib_metadata.distributions():
try:
relative = pathlib.Path(__file__).relative_to(dist.locate_file(''))
except ValueError:
pass
else:
if relative in dist.files:
return dist
return None
project_distribution = get_project_distribution()
if project_distribution:
project_name = project_distribution.metadata['Name']
version = project_distribution.metadata['Version']