替换 pkg_resources `Distribution.run_script()`

问题描述 投票:0回答:1

由于 pkg_resources 已被弃用,但官方迁移指南未提供其所有 API 的迁移路径,我正在寻找

Distribution.run_script()
的替代品。

据我所知,它的唯一目的是获取

scripts
中附带的脚本,然后从磁盘读取它或使用
Distribution.get_metadata()
获取它,然后在提供的命名空间中执行它。所以我的问题是:

  1. 有现成的替代品吗(我想没有)?
  2. get_metadata()
    代码路径是什么,是为了压缩鸡蛋还是出于其他原因?由于其他代码路径更容易替换,因此是否有可用的替代代码路径?或者它根本不相关?
python pkg-resources
1个回答
0
投票

这是我使用的替代品,它可能不完整,但它适用于拉链鸡蛋。不幸的是,它使用了私有属性,但我没有看到公开 API 公开

_path

def _run_script(dist, script_name, namespace):
    # an importlib-based replacement for pkg_resources.NullProvider.run_script()
    script = 'scripts/' + script_name
    source = dist.read_text(script)
    if not source:
        raise ValueError(
            f"Script {script!r} not found in metadata at {dist._path!r}"
        )
    script_filename = dist._path.joinpath(script)
    code = compile(source, str(script_filename), 'exec')
    exec(code, namespace, namespace)
© www.soinside.com 2019 - 2024. All rights reserved.