简体   繁体   English

使用 Poetry 对“找不到分发”pkg_resources 进行故障排除

[英]Troubleshooting 'distribution not found' pkg_resources with Poetry

I am building a cli application, in which I am using poetry and pyproject.toml to define my application's version:我正在构建一个 cli 应用程序,在其中我使用诗歌pyproject.toml来定义我的应用程序的版本:

[tool.poetry]
name = "fili"
version = "0.1.0"
description = "My super awesome program."
authors = ["Jacob Pavlock <jtpavlock@gmail.com>"]
license = "MIT"

Now, I'd like to programatically access this version number in the CLI for my application, so I added the following code to cli.py现在,我想在 CLI 中为我的应用程序以编程方式访问此版本号,因此我将以下代码添加到cli.py

import argparse
import pkg_resources


def main():
    """Run our cli."""
    VERSION = pkg_resources.get_distribution("fili").version

    fili_parser = argparse.ArgumentParser(description="Run fili.")
    fili_parser.add_argument(
        "--version", action="version", version="%(prog)s {0}".format(VERSION)
    )


if __name__ == "__main__":
    main()

However, when run, I get the following error:但是,在运行时,我收到以下错误:

$ ./fili/cli.py 
Traceback (most recent call last):
  File "./fili/cli.py", line 22, in <module>
    main()
  File "./fili/cli.py", line 11, in main
    VERSION = pkg_resources.get_distribution("fili").version
  File "/home/jacob/.cache/pypoetry/virtualenvs/fili-yAte7WxV-py3.8/lib/python3.8/site-packages/pkg_resources/__init__.py", line 481, in get_distribution
    dist = get_provider(dist)
  File "/home/jacob/.cache/pypoetry/virtualenvs/fili-yAte7WxV-py3.8/lib/python3.8/site-packages/pkg_resources/__init__.py", line 357, in get_provider
    return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  File "/home/jacob/.cache/pypoetry/virtualenvs/fili-yAte7WxV-py3.8/lib/python3.8/site-packages/pkg_resources/__init__.py", line 900, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/home/jacob/.cache/pypoetry/virtualenvs/fili-yAte7WxV-py3.8/lib/python3.8/site-packages/pkg_resources/__init__.py", line 786, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'fili' distribution was not found and is required by the application

For reference, my project directory structure供参考,我的项目目录结构

$ tree
.
├── fili
│   ├── cli.py
│   ├── __init__.py
│   └── __pycache__
│       ├── cli.cpython-38.pyc
│       └── __init__.cpython-38.pyc
├── fili.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
├── LICENSE
├── poetry.lock
├── pyproject.toml
└── README.md

I have installed my package with poetry install , but what else am I missing?我已经安装了我的 package 与poetry install ,但我还缺少什么?

You have to make sure that the environment that poetry installed it into when you ran poetry install is also the one that is executed when you run ./fili/cli.py .您必须确保在运行poetry install时将poetry安装到的环境也是在运行./fili/cli.py时执行的环境。 The latter command will just grab whatever is registered as the system's python binary to run your script, while poetry install will try to install it into whichever virtual environment is activated - or create a new one if none is.后一个命令将仅获取注册为系统python二进制文件的任何内容来运行您的脚本,而poetry install将尝试将其安装到任何激活的虚拟环境中 - 如果没有,则创建一个新环境。

To solve this issue, you can 1) run poetry shell in order to activate the virtual environment that poetry used, after which a call like fili/cli.py will use the activated python , or you 2) delegate the call explicitly with poetry run fili/cli.py instead.要解决此问题,您可以 1)运行poetry shell以激活poetry使用的虚拟环境,之后像fili/cli.py这样的调用将使用激活的python ,或者您 2)使用poetry run fili/cli.py显式委托调用poetry run fili/cli.py代替。


If this didn't solve the problem, there are two other issues that might be the cause.如果这不能解决问题,则可能还有其他两个问题。 The first is that you're using pkg_resources with python3.8.首先是您将pkg_resources与 python3.8 一起使用。 You shouldn't do that, the standard library was extended with importlib.metadata.version , which is just a straight upgrade.你不应该这样做,标准库是用importlib.metadata.version扩展的,这只是一个直接的升级。

And finally, running part of an installed package as a script (read, directly executing a.py file instead of, say, a module: python -m fili ) is a bit unsafe.最后,将已安装的 package 的一部分作为脚本运行(读取,直接执行 a.py 文件而不是模块: python -m fili )有点不安全。 It might mess up imports in confusing ways, which is usually solved by defining entrypoints for scripts .它可能会以令人困惑的方式混淆导入,这通常通过为 scripts 定义入口点来解决。

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

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