poetry install | SolverProblemError 因为 my_project 依赖于 (*) 版本不匹配的 string 库,导致版本无法解决。

17

我还没有使用诗歌来运行项目,所以请原谅我的不理解。

我成功安装了诗歌 Python 库管理器,使用以下命令:

curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python3

接下来的步骤是,首先运行poetry install命令,但是可能会返回以下错误:

me@LAPTOP-G1DAPU88:~/.ssh/workers-python/workers$ poetry install

  RuntimeError

  Poetry could not find a pyproject.toml file in /home/me/.ssh/workers-python/workers or its parents

  at ~/.poetry/lib/poetry/_vendor/py3.8/poetry/core/factory.py:369 in locate
      365if poetry_file.exists():
      366return poetry_file
      367368else:
    → 369raise RuntimeError(
      370"Poetry could not find a pyproject.toml file in {} or its parents".format(
      371│                     cwd
      372│                 )
      373│             )

我很快意识到我需要自己制作一个 pyproject.toml 文件。再次运行poetry install,结果如下:

$ poetry install

  TOMLError

  Invalid TOML file /home/me/.ssh/workers-python/workers/pyproject.toml: Key "json " already exists.

  at ~/.poetry/lib/poetry/_vendor/py3.8/poetry/core/toml/file.py:34 in read
      30def read(self):  # type: () -> "TOMLDocument"
      31try:
      32return super(TOMLFile, self).read()
      33except (ValueError, TOMLKitError) as e:
    → 34raise TOMLError("Invalid TOML file {}: {}".format(self.path.as_posix(), e))
      3536def __getattr__(self, item):  # type: (str) -> Any
      37return getattr(self.__path, item)
      38

以上错误表明存在重复的条目。
使用现在已更新的pyproject.toml文件在cwd中再次运行poetry install时,会出现本文标题中的错误。请注意,html标签保留不变。
$ poetry install
Creating virtualenv my_project-1_EUeV5I-py3.8 in /home/me/.cache/pypoetry/virtualenvs
Updating dependencies
Resolving dependencies... (28.4s)

  SolverProblemError

  Because my_project depends on string (*) which doesn't match any versions, version solving failed.

  at ~/.poetry/lib/poetry/puzzle/solver.py:241 in _solve
      237│             packages = result.packages
      238│         except OverrideNeeded as e:
      239│             return self.solve_in_compatibility_mode(e.overrides, use_latest=use_latest)
      240│         except SolveFailure as e:
    → 241│             raise SolverProblemError(e)
      242│
      243│         results = dict(
      244│             depth_first_search(
      245│                 PackageNode(self._package, packages), aggregate_package_nodes

然而,暂时删除所有实例=“*”后,我在第12行遇到了\n错误...但那里似乎没有这个错误:

$ poetry install

  TOMLError

  Invalid TOML file /home/me/.ssh/workers-python/workers/pyproject.toml: Unexpected character: '\n' at line 12 col 5

  at ~/.poetry/lib/poetry/_vendor/py3.8/poetry/core/toml/file.py:34 in read
      30def read(self):  # type: () -> "TOMLDocument"
      31try:
      32return super(TOMLFile, self).read()
      33except (ValueError, TOMLKitError) as e:
    → 34raise TOMLError("Invalid TOML file {}: {}".format(self.path.as_posix(), e))
      3536def __getattr__(self, item):  # type: (str) -> Any
      37return getattr(self.__path, item)
      38│
me@LAPTOP-G1DAPU88:~/.ssh/workers-python/workers$ cat pyproject.toml
[tool.poetry]
name = "my_project"
version = "0.1.0"
description = "Top-level package for my_project."
authors = [""]
packages = [
    { include = "my_project"},
]

[tool.poetry.dependencies]
python = "^3.8"
click  # Suspect line

我已经撤销了这个操作。

当前的 pyproject.toml 文件:

[tool.poetry]
name = "data_simulator"
version = "0.1.0"
description = "Top-level package for data_simulator."
authors = ["iotahoe <iotahoe@iotahoe.com>"] # daniel.bell@hitachivantara.com / daniel@iotahoe.com
packages = [
    { include = "data_simulator"},
]

[tool.poetry.dependencies]
python = "^3.8"
click = "*"
#logging = "*"
#os = "*"
#pathlib = "*"
#time = "*"
numpy = "*"
pandas = "*"
#json = "*"
#random = "*"
faker = "*"
transformers = "4.4.2"
#re = "*"
#itertools = "*"
#datetime = "*"
#requests = "*"
#copy = "*"
#collections = "*"
#collections.abc = "*"
#multiprocessing = "*"
#multiprocessing.dummy = "*"
nltk = "*"
#nltk.corpus = "*"
#string = "*"

[tool.poetry.dev-dependencies]
isort = "5.6.4"
black = "^20.8b1"
invoke = "^1.4.1"
coveralls = "^2.2.0"
pytest = "^3.0"
flake8 = "^3.8.3"
mypy = "^0.782"

[[tool.poetry.source]]
name = "azure"
url = "https://pkgs.dev.azure.com/iotahoe/Halo/_packaging/private-sources/pypi/simple/"
secondary = true

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"

注意:'name'、'authors'、'include' 和 'url' 已被审查。
注意:此处有已被审查的信息。

如何表示软件包的最新版本?我以为使用“*”可以实现。 - user16341274
5个回答

20
作为一般建议,我建议使用Poetry的命令行而不是创建或操作pyproject.toml文件。可以通过使用 "poetry init" 或 "poetry init -n" 命令来开始,然后使用 "poetry add" 命令添加所需依赖。
当前 pyproject.toml 文件的问题在于您将内置包(如 os、pathlib 和 string 等)声明为依赖项。这就是为什么您会收到 "Because my_project depends on string (*) which doesn't match any versions, version solving failed." 的错误消息,这意味着 Poetry 无法在存储库中找到任何匹配的软件包信息。

如何判断一个包是否为内置包?抱歉我是个新手。 - user16341274
运行 poetry init -n,然后安装 poetry add click。然而,运行 poetry add logging 抛出了 EnvCommandError。运行 poetry add os 抛出了 ValueError - user16341274
1
你可以在这里找到内置模块的列表(https://docs.python.org/3/py-modindex.html)。我想知道你是如何收集依赖列表的。通常在编程过程中,你应该知道是否需要安装依赖项或者它是否可以直接使用。 - finswimmer
标准的 poetry init 命令为我解决了这个问题,生成了一个 .lock 文件。非常感谢。 - user16341274
修改 pyproject.toml 没有任何问题。锁定文件应该是自动生成的。你可以使用 update 命令来更新版本,但是还有很多需要手动设置的配置,比如 [tool.*] 配置。 - Sonic Soul
显示剩余4条评论

3
删除旧的 poetry.lock 对我来说解决了问题。

2

简而言之:在运行poetry lock之前清空*.egg-info目录。

虽然这个答案与当前问题没有直接关系,但在其他情况下可能会出现类似的错误信息,因此我认为在这里分享是有价值的。

如果您正在锁定一个项目,在该项目的子依赖项直接可用于文件系统的情况下,一些*.egg-info目录可能会干扰锁定过程,在缺少这些*.egg-info文件的情况下尝试运行poetry install时会导致问题。为避免出现问题:在锁定之前清空*.egg-info目录。然后,您应该拥有一个更新的poetry.lock文件,并且其中包含更多内容。


2
希望能得到一些关于如何在本地文件系统中“清除egg-info”的实用建议! - Dima Tisnek
1
在当前工作目录及其所有子目录中查找所有 egg-info 并将其删除。命令为:find . -name *.egg-info | xargs rm -r - Finch_Powers

1
尝试删除 poetry.lock 文件,然后运行命令 poetry install 来创建一个新的 poetry 文件。这个方法对我也有效。

这并没有回答问题。一旦你拥有足够的声望,你就可以评论任何帖子;相反,提供不需要提问者澄清的答案。- 来自审核 - Poulpynator
根据目前的写法,你的回答不够清晰。请编辑以添加更多细节,帮助其他人理解这如何回答所提出的问题。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

1

尝试删除 poetry.lock 文件,然后运行命令 poetry install 来创建新的 poetry 文件。对我有效。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接